Page 1 of 2

3280

Posted: Wed Apr 13, 2011 10:49 pm
by pedromalta
Anyone did it in a better way?!?! SUre you did!

Hi guys, this is pretty much my first post here! Found this site yesterday, and made a lot of the challenge in some hours!

I did the RFC 2280 one like that:

On KDE Kate i replaced all spaces for break lines, and got a line by line text file, then removed all the garbage, like " / , . ; : { } and others, then put it all in lowercase, easy stuff, i made this for the other ones befores this challenge as well

Then i made this "MacGyver Oriented Program", it don't accurate count the times(as i tested it with cat wordlist | grep -c answer), but it gave the answer, that's what i needed, so i'll think why it don't give the accurate count later, or you could point it to me!

Well... here it is.. MacGyver Style C programming!

(I'm brazilian, sorry if the variables/comments don't make any sense...)

#include <stdio.h>
#include <stdlib.h>

#define SIZE 100
#define MAXPALAVRA 99000

//alterar esses defines abaixo para o seu uso
#define ARQUIVO "wordlist" //o nome do arquivo, tem que estar na mesma pasta
#define TAMPALAVRA 9 //define o tamanho em caracteres da palavra


typedef struct
{
char palavra[SIZE];
int count;
}WORD;

void criaPalavra(WORD *novaPal, char* word)
{
strcpy(novaPal->palavra, word);
novaPal->count=0;
}

//cria uma lista com a palavra e um contador

typedef struct
{
WORD lista[MAXPALAVRA];
int len;
}LISTADEPALAVRAS;


void addPalavra(LISTADEPALAVRAS *list, WORD pal, int posi)
{
list->lista[posi] = pal;
list->len = 0;
}


void maisAparece(LISTADEPALAVRAS *list)
{
WORD atual, comparada, maisAparece;
int i = 0,
j = 0;

while (i < list->len)
{
atual = list->lista;

j = i + 1;
while(j < list->len)
{

comparada=list->lista[j];
if (!strcmp(atual.palavra,comparada.palavra))
{
list->lista[j].count++;
}
j++;

}
i++;


}



maisAparece = list->lista[0];
i = 1;
while (i < list->len)
{
atual = list->lista;

if (atual.count > maisAparece.count)
{
maisAparece = atual;
}
i++;
}

printf("%d - %s",maisAparece.count,maisAparece.palavra);

}

//cria uma lista com a lista anterior

main()
{
FILE *arquivo;
int index = 0;
LISTADEPALAVRAS listap;
arquivo=fopen(ARQUIVO,"r");

while (!feof(arquivo))
{
char lida[100] = "";
fscanf(arquivo, "%s\n", &lida);
if(strlen(lida) == TAMPALAVRA)
{
WORD pal;

criaPalavra(&pal, lida);
addPalavra(&listap, pal, index);
//printf("%d - %s\n",index, listap.lista[index].palavra);
index++;

}
}
listap.len = --index;
maisAparece(&listap);
//printf("%d",index);
}

Posted: Thu Apr 14, 2011 7:13 pm
by wodkahack0r
There is always more than one way to do things ;-) Here is mine:

59.pl:

Code: Select all

#!/usr/bin/perl
open (IN,"rfc3280.txt");
my $i;
while (<IN>){
	@arr = split / /, $_;
	for ($i=0;$i<=scalar(@arr);$i++) {
		if ($arr[$i] =~ /^\w{9}$/) {
			print $arr[$i]."\n";
		}
	}
}
close (IN);
Followed by bash & co:

Code: Select all

perl 59.pl|sort|uniq -c | sort -n|tail -n1

Posted: Thu Apr 14, 2011 8:25 pm
by pedromalta
Nice, i don't know a thing of pearl, but i've seen such little codes when applyed to strings, must be a great language for those ocasions!

but very unreadable for the normal people too!

Sure i'll learn it someday!

Thanks for posting your aproach!

now i see theres a "Code" option here... perharps ill post my code again with that

Posted: Thu Apr 14, 2011 8:33 pm
by pedromalta
Since i couldn't edit the first post...

Code: Select all


#include <stdio.h>
#include <stdlib.h>

#define SIZE 100
#define MAXPALAVRA 99000

//alterar esses defines abaixo para o seu uso
#define ARQUIVO "wordlist" //o nome do arquivo, tem que estar na mesma pasta
#define TAMPALAVRA 9 //define o tamanho em caracteres da palavra


typedef struct
{
    char palavra[SIZE];
    int count;
}WORD;

void criaPalavra(WORD *novaPal, char* word)
{
    strcpy(novaPal->palavra, word);
    novaPal->count=0;
}

//cria uma lista com a palavra e um contador

typedef struct
{
    WORD lista[MAXPALAVRA];
    int len;
}LISTADEPALAVRAS;


void addPalavra(LISTADEPALAVRAS *list, WORD pal, int posi)
{
    list->lista[posi] = pal;
    list->len = 0;
}


void maisAparece(LISTADEPALAVRAS *list)
{
    WORD atual, comparada, maisAparece;
    int i = 0,
        j = 0;

    while (i < list->len)
    {
       atual = list->lista[i];

       j = i + 1;
       while(j < list->len)
       {

           comparada=list->lista[j];
           if (!strcmp(atual.palavra,comparada.palavra))
            {
               list->lista[j].count++;
            }
            j++;

       }
       i++;


    }



    maisAparece = list->lista[0];
    i = 1;
    while (i < list->len)
    {
        atual = list->lista[i];

        if (atual.count > maisAparece.count)
        {
            maisAparece = atual;
        }
        i++;
    }

    printf("%d - %s",maisAparece.count,maisAparece.palavra);

}

//cria uma lista com a lista anterior

main()
{
    FILE *arquivo;
    int index = 0;
    LISTADEPALAVRAS listap;
    arquivo=fopen(ARQUIVO,"r");

    while (!feof(arquivo))
    {
        char lida[100] = "";
        fscanf(arquivo, "%s\n", &lida);
        if(strlen(lida) == TAMPALAVRA)
        {
            WORD pal;

            criaPalavra(&pal, lida);
            addPalavra(&listap, pal, index);
            //printf("%d - %s\n",index, listap.lista[index].palavra);
            index++;

        }
    }
   listap.len = --index;
   maisAparece(&listap);
   //printf("%d",index);
}

Posted: Wed May 18, 2011 1:28 pm
by Mokor
My solution in Groovy:

Code: Select all

def words = new File("rfc3280.txt").getText().split()

def wordSet = words as Set
wordSet = wordSet.findAll { w -> w.size() == 9 }

def textMap = [:]
wordSet.each { textMap.put(it, words.count(it)) }
def result = textMap.values().max()
println textMap.findAll { it.value == result }
I think you could improve the last 4 lines, but it works.

Posted: Fri May 20, 2011 8:32 am
by Filzkritik
Not really coding, but at least something that worked as expected right from the start:

grep -oiE " [a-z]{9} " rfc.txt |sort|uniq -c |sort

Posted: Fri May 27, 2011 9:16 pm
by L0RI
I used this Site:
http://elmar-eigner.de/word-count.html
Then, I put the result in Excel and sorted it by length.

...

Posted: Sun Jul 31, 2011 1:56 pm
by rain1024
I finally got it, without any code :D

There are many way to get answer. I use a regex software and excel to find max

Hope all of you can solve it easily
L0RI wrote:I used this Site:
http://elmar-eigner.de/word-count.html
Then, I put the result in Excel and sorted it by length.
@Lori: it's a nice way. You're a talented man . :wink:

Posted: Sat Sep 03, 2011 9:41 am
by laz0r
Go Mathematica :)

Commonest[
Select[StringCases[Import["http://www.ietf.org/rfc/rfc3280.txt"], WordCharacter ..], StringLength[#] == 9 &]]

Posted: Thu Sep 22, 2011 9:34 pm
by turing_bot

Code: Select all


import java.io.*;
import org.apache.commons.lang3.StringUtils;

public class tan {
	private static int words, countPrevious, countNow = 0;
	private static String line, stringOfWords, mostFrequentWord = " ";
	private static String[] linesplit = new String[2000];

	public static void countWords() {
		linesplit = line.split(" ");
		for (int i = 0; i < linesplit.length; i++) {
			if (linesplit[i].length() == 9) {
				words++;
				stringOfWords = stringOfWords + " " + linesplit[i];

			}
		}
	}

	public static void searchFrequent() {
		System.out.println(stringOfWords);
		String[] arrayOfWords = stringOfWords.split(" ");
		for (int i = 0; i < arrayOfWords.length; i++) {

			String searchword = arrayOfWords[i];
			countNow = 0;
			countNow = StringUtils.countMatches(stringOfWords, searchword);

			if (countNow >= countPrevious) {
				countPrevious = countNow;
				mostFrequentWord = searchword;
				System.out.println("Word: " + mostFrequentWord + " prev: "
						+ countPrevious + " now: " + countNow);
			}

		}

	}

	public static void main(String[] args) {
		try {
			FileReader input = new FileReader(
					"C:\\Users\\Daniel\\Documents\\Studie\\CODA\\Practicum\\server\\RFC.txt");
			BufferedReader bufRead = new BufferedReader(input);

			line = bufRead.readLine();

			while (line != null) {
				countWords();
				line = bufRead.readLine();
			}
			searchFrequent();

			System.out.println("This document has " + words
					+ " words of nine characters long.");
			System.out.println("According to this sucky program, the word "
					+ mostFrequentWord + " is most frequent, ");
			System.out.println("occurring " + countPrevious + " times.");
			bufRead.close();

		} catch (ArrayIndexOutOfBoundsException e) {
			/*
			 * If no file was passed on the command line, this exception is
			 * generated. A message indicating how to the class should be called
			 * is displayed
			 */
			System.out.println("Usage: java tan filename\n");

		} catch (IOException e) {
			// If another exception is generated, print a stack trace
			e.printStackTrace();
		}

	}
}
Urgh. This took me too long :P Finally got it, though!

Those tiny grep and pearl programmes make me feel bad... :( :P

Posted: Wed Oct 12, 2011 6:22 pm
by mauricemoss
Here my solution in python:

Code: Select all

text = open('rfc3280.txt').read().lower().replace('.', '').replace(',', '')
textlist = text.split()
niner_list = []
niner_dic = {}

for word in textlist:
	if len(word) == 9:
		niner_list.append(word)
		
for word in niner_list:
	niner_dic[niner_list.count(word)] = word
	
print niner_dic[max(niner_dic.keys())]
[/quote]

Posted: Mon Jan 23, 2012 11:52 pm
by wynk
My solution in ruby:

Code: Select all

file = File.new("rfc3280.txt", "r")
dict = Hash.new(0)
while (line = file.gets)
	line.scan(/[^\w]([\w]{9})[^\w]/) { |match|
		dict.store(match, dict[match].succ)
	}
end
puts dict.index(dict.values.max)
file.close

Posted: Wed Jun 13, 2012 1:51 pm
by facetoe
Another Ruby solution:

Code: Select all

words = {}

File.open('file', 'r').readlines.each do |line|
  line.split.each do |word|
    if word.size == 9
      if words[word]==nil
        words.merge!(Hash[word=>0])
      else
        words[word]+=1
      end
    end
  end
end

puts words.max_by{|k,v|v}

Posted: Fri Nov 29, 2013 4:45 pm
by kra3

Code: Select all

#! python 

from collection import Counter
data = open('rfc3280').read()
print Counter(i for i in data.split() if len(i)==9).most_common(1)[0][0]


Posted: Mon Dec 16, 2013 10:13 pm
by FlippyDeath
I entered the first nine letter word I could find in the text and it was right...

My luck...