Lorem Ipsum

Discussion of challenges you have already solved
User avatar
awf
Posts: 3
Joined: Sat May 07, 2011 7:20 pm

Post 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)
Millennium
Posts: 17
Joined: Thu Apr 21, 2011 3:08 am

Post by Millennium »

I honestly did this one in about 2 seconds using an online keyword frequency analyzer. :?
Mokor
Posts: 4
Joined: Tue May 17, 2011 10:51 am
Location: Germany

Post 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} )
User avatar
lamedog
Posts: 3
Joined: Mon Jul 25, 2011 12:00 pm

Post 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)
strongdrink
Posts: 4
Joined: Tue Jul 12, 2011 4:31 pm
Location: Floating Around

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

Post by laz0r »

Again, go Mathematica:
First@First@
SortBy[Tally[
StringCases[
Import["http://www.hacker.org/challenge/misc/lorem.txt"],
WordCharacter ..]], #[[2]] &]
There is no spoon.
AMindForeverVoyaging
Forum Admin
Posts: 496
Joined: Sat May 28, 2011 9:14 am
Location: Germany

Post by AMindForeverVoyaging »

You didn't solve this challenge just now, did you? ;)
User avatar
laz0r
Posts: 290
Joined: Thu Feb 04, 2010 4:18 pm
Location: Within the depths of Unix

Post by laz0r »

AMindForeverVoyaging wrote:You didn't solve this challenge just now, did you? ;)
Dammit, caught out :wink:
There is no spoon.
mauricemoss
Posts: 3
Joined: Fri Oct 07, 2011 12:27 pm

Post 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)
F1re223
Posts: 2
Joined: Wed Nov 16, 2011 12:20 pm

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

Post 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
marseel
Posts: 1
Joined: Fri Nov 29, 2013 10:58 am

Post by marseel »

Code: Select all

tr -cs 'a-zA-Z' '\n' < input | sort | uniq -c | grep " 1 "
BITCHES
CptObvious
Posts: 3
Joined: Wed Apr 16, 2014 2:53 pm

Post 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;
	}
}
wired_devil
Posts: 1
Joined: Fri May 02, 2014 9:08 pm

Post 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]
InyaGaming
Posts: 2
Joined: Fri Sep 12, 2014 8:53 am
Contact:

Post 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;
	}
}
Post Reply