Page 2 of 3

Posted: Sun May 08, 2011 4:16 pm
by awf

Code: Select all

def wordcount(path)
file=File.open(path);a=IO.read(file).split(" ");b=[[a[0],1]];
for i in 1..a.length-1
	for j in 0..b.length-1
		if a[i].chomp(",").chomp(".")==b[j][0].chomp(",").chomp(".") then
			b[j][1]=b[j][1]+1;c=false;break;
		else
			c=true;
		end;
	end;
	if c==true then
		c=false;b[b.length]=[a[i].chomp(",").chomp("."),1]
	end;
end;
return(b);
end;
p(wordcount("lorem.txt"));
I wrote a Ruby program which returns an array of all different words (by ignoring dots or commata) with their count for a certain file path. It's not that nice but it works 8)

Posted: Mon May 09, 2011 8:53 pm
by Millennium
I honestly did this one in about 2 seconds using an online keyword frequency analyzer. :?

Posted: Wed May 18, 2011 11:51 am
by Mokor
I wanted to solve it with Groovy. Here is the result:

Code: Select all

def words = new File("lorem.txt").getText().split()
words.each( {if (words.count(it) == 1) println it} )

Posted: Tue Jul 26, 2011 1:30 am
by lamedog
The Python console style

Code: Select all

raw="""
<PASTE THE TEXT HERE>
"""
# drop ponctuation
txt = raw.replace('.', ' ').replace(',', ' ')

# go get the word
list = txt.split()
word = [x for x in list if list.cont(x) == 1]

# here you are!!!
print(word)

Posted: Fri Sep 02, 2011 7:58 pm
by strongdrink
I just whipped up a ksh script in 5 minutes that counts the occourances of each word (mind you its shotty and slow :P)

Code: Select all

#!/bin/ksh

string=$@
set -A words $string

for word in ${words[@]}; do
	
	count=0

	for check in ${words[@]}; do
		
		if [[ "$check" = "$word" ]]; then
			
			let count=$count+1
			
		fi
		
	done
	
	echo $word - $count

done

echo ${array[*]}

exit
and then ran the command

./word_amount.ksh `cat lorem` | grep -w "1"

piping + grep ftw :D

Posted: Sat Sep 03, 2011 9:46 am
by laz0r
Again, go Mathematica:
First@First@
SortBy[Tally[
StringCases[
Import["http://www.hacker.org/challenge/misc/lorem.txt"],
WordCharacter ..]], #[[2]] &]

Posted: Sat Sep 03, 2011 10:04 am
by AMindForeverVoyaging
You didn't solve this challenge just now, did you? ;)

Posted: Sat Sep 03, 2011 7:22 pm
by laz0r
AMindForeverVoyaging wrote:You didn't solve this challenge just now, did you? ;)
Dammit, caught out :wink:

Posted: Fri Oct 07, 2011 3:44 pm
by mauricemoss
That Python code did if for me:

Code: Select all

text = open('lorem.txt').read().lower().replace('.', '').replace(',', '')
textlist = text.split()
for word in textlist:
	if text.count(word) == 1:
		print(word)

Posted: Wed Jan 04, 2012 8:20 am
by F1re223
Quick and dirty, but it works.
Java:

Code: Select all

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(
                                               new InputStreamReader(new FileInputStream("lorem.txt"))); 
		StringBuffer contentOfFile = new StringBuffer();
		String line; 
		while ((line = br.readLine()) != null) contentOfFile.append(line);
		
		String[] content = contentOfFile.toString()
                                           .replace('.', ' ').replace(',', ' ').replace(';', ' ').split(" ");
		
		for(int i=0; i < content.length; i++) {
			int tmpCounter = 0;
			String tmpWord = content[i];
			for(int y = 0; y < content.length; y++)
				if (tmpWord.equalsIgnoreCase(content[y])) tmpCounter++;
			if(tmpCounter == 1) System.out.println(tmpWord);
		}
	}

Posted: Wed Jun 13, 2012 2:48 pm
by facetoe
Easy as pie:

Code: Select all

words = {}

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

puts words.sort_by {|k,v|v}.first

Posted: Thu Dec 19, 2013 11:23 pm
by marseel

Code: Select all

tr -cs 'a-zA-Z' '\n' < input | sort | uniq -c | grep " 1 "
BITCHES

Posted: Fri Apr 25, 2014 11:42 am
by CptObvious
Java:

Code: Select all

String challenge = "put lorem ipsum here";
for (String s : challenge.split(" ")){
	if (challenge.indexOf(s) == challenge.lastIndexOf(s)){
		System.out.println(s);
		break;
	}
}

Posted: Wed May 07, 2014 12:28 am
by wired_devil
This is what i did, also this code works for others challenges...

Code: Select all

a = open('lorem.txt').read().replace('.', '').replace(',', '').replace('\n', '').replace('\r', '').replace(':', '')
y = a.split()
l = {}
for k in y:
    if l.has_key(k):
        l.update({k: l[k]+1})
    else:
        l.update({k: 1})
print sorted(l.items(), key=lambda x:x[1])
[/code]

Posted: Tue Dec 02, 2014 2:48 pm
by InyaGaming
PHP:

Code: Select all

<?php
$text = "TEXT HERE"
$t_array = explode(" ", $text);
$all = array();
foreach($t_array as $word){
	$word = str_replace(".", "", $word);
	$word = str_replace(",", "", $word);
	if(isset($all[$word])){
		$all[$word] += 1;
	} else {
		$all[$word] = 1;
	}
}
foreach($all as $word => $e){
	if($e == 1){
		echo $word;
	}
}