3280

Discussion of challenges you have already solved

This Code sucks?!

A lot!
6
38%
Not that much...
6
38%
Quite decent for a first year college guy...
4
25%
 
Total votes: 16

pedromalta
Posts: 22
Joined: Wed Apr 13, 2011 12:00 am
Location: Vila Velha
Contact:

3280

Post 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);
}
sudo apt-get a life
wodkahack0r
Posts: 9
Joined: Thu Mar 05, 2009 7:27 pm

Post 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
pedromalta
Posts: 22
Joined: Wed Apr 13, 2011 12:00 am
Location: Vila Velha
Contact:

Post 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
sudo apt-get a life
pedromalta
Posts: 22
Joined: Wed Apr 13, 2011 12:00 am
Location: Vila Velha
Contact:

Post 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);
}
sudo apt-get a life
Mokor
Posts: 4
Joined: Tue May 17, 2011 10:51 am
Location: Germany

Post 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.
Filzkritik
Posts: 1
Joined: Fri May 06, 2011 7:00 am

Post 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
L0RI
Posts: 2
Joined: Fri May 27, 2011 10:38 am

Post 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.
rain1024
Posts: 27
Joined: Sat Jul 23, 2011 5:20 pm

...

Post 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:
User avatar
laz0r
Posts: 290
Joined: Thu Feb 04, 2010 4:18 pm
Location: Within the depths of Unix

Post by laz0r »

Go Mathematica :)

Commonest[
Select[StringCases[Import["http://www.ietf.org/rfc/rfc3280.txt"], WordCharacter ..], StringLength[#] == 9 &]]
There is no spoon.
turing_bot
Posts: 2
Joined: Wed Feb 23, 2011 3:01 pm

Post 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
mauricemoss
Posts: 3
Joined: Fri Oct 07, 2011 12:27 pm

Post 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]
wynk
Posts: 7
Joined: Tue Jan 03, 2012 7:59 pm

Post 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
facetoe
Posts: 3
Joined: Wed Jun 13, 2012 6:56 am

Post 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}
kra3
Posts: 2
Joined: Fri Nov 29, 2013 11:46 am

Post 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]

FlippyDeath
Posts: 1
Joined: Mon Dec 16, 2013 4:27 pm

Post by FlippyDeath »

I entered the first nine letter word I could find in the text and it was right...

My luck...
Post Reply