Welcome, Guest. Please login or register.

Author Topic: My C homework  (Read 15965 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline AmireX

  • Jr. Member
  • **
  • Join Date: May 2005
  • Posts: 72
    • Show all replies
Re: Mel's C homework
« on: February 27, 2007, 09:51:19 PM »
Quote

mel_zoom wrote:
Code: [Select]

/* fill a range bucket based on the word length*/
if (length<4) {
/* for lengths < 4 fill bucket 0 */
lengths[0]++;
}
else if (length<11) {
/* for lengths 4...10 fill buckets 1-7*/
lengths[length-3]++;
} else {
/* for lengths > 10 fill bucket 8*/
lengths[8]++;




Not even faster, but more compact :-)
Code: [Select]

/* fill a range bucket based on the word length*/
                                length = min(max(length-3,0),8);
                                lengths[length]++;


with
Code: [Select]

#define min(a, b)  (((a) < (b)) ? (a) : (b))
#define max(a, b)  (((a) > (b)) ? (a) : (b))

Regards, ReX
 

Offline AmireX

  • Jr. Member
  • **
  • Join Date: May 2005
  • Posts: 72
    • Show all replies
Re: Mel's C homework
« Reply #1 on: March 01, 2007, 06:58:10 PM »
Quote

guru-666 wrote:
wow so many ppl interested in some average chicks c homework, amazing....  You guy's know that python has this kind of function built in already.


Yes python is the better choice :-), could look like this:

Code: [Select]

import sys

argc = len(sys.argv)
if argc < 2:
  print &quot;Usage: wordcount <file name>&quot;
  sys.exit(1)

word_l = open(sys.argv[0]).read().split()
len_d = {}
for word in word_l:
  size = len(word)
  if size in range(1,4): size = 3
  if size > 10:          size = 10
  if len_d.has_key(size): len_d[size] += 1
  else:                   len_d[size] = 1
print len_d

 

Offline AmireX

  • Jr. Member
  • **
  • Join Date: May 2005
  • Posts: 72
    • Show all replies
Re: Mel's C homework
« Reply #2 on: March 01, 2007, 09:59:33 PM »
Quote

mel_zoom wrote:
As far as I understand Java, Perl, Python, PHP and all of the .net languages are all implemented in C at some level and cannot be built without it!

When everybody has forgotten C who will actually maintain the languages they have migrated to?


That's true. I use python a lot, but to integrate new functions into the language I often need to extend the interpreter with so called python dlls .. written in plain C ;-)

But at the other hand I try to not reinvent the wheel (is this correct english ??), so different languages for different tasks. But I like C/C++ too, it's like a good sudoku.
 

Offline AmireX

  • Jr. Member
  • **
  • Join Date: May 2005
  • Posts: 72
    • Show all replies
Re: Mel's C homework
« Reply #3 on: March 01, 2007, 10:07:58 PM »
Quote

guru-666 wrote:
perfect english! well said.


Danke, I'm learning.
 

Offline AmireX

  • Jr. Member
  • **
  • Join Date: May 2005
  • Posts: 72
    • Show all replies
Re: Mel's C homework
« Reply #4 on: March 01, 2007, 10:24:07 PM »
To solve it in C (and not C++) you can use a hash function. Such a function calculates a special hash value (in a defined range) for every string. This value you can use to put the strings into an array. To avoid conflicts of equal hash values for different strings you built an array of linked lists and appends the equal strings to this lists.

Depending of the size of your "string list array" you have an access near to 1 O(1) to find a string.  But this is a hash map and always implemented in the standard template library for example.  :-?