Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming

Reply
 
LinkBack Thread Tools
Old 10-30-08   #91 (permalink)
Programmer
 
xtascox's Avatar
 
intel nvidia

Join Date: Jul 2007
Location: Dallas, PA
Posts: 778

Rep: 37 xtascox is acknowledged by some
Unique Rep: 36
Trader Rating: 1
Default

Hm looks like fun. Looking forward to the next challenge.
__________________

System: SVT
CPU
Q6600 @ 3.2ghz 1600fsb
Motherboard
EVGA 780i FTW
Memory
4GB OCZ Platimum
Graphics Card
2X EVGA 8800GTS 512
Hard Drive
160GB Seagate Barracuda 7200 SATA
Sound Card
Onboard Realtek ALC883
Power Supply
CORSAIR 750TX
Case
Chenming 901A
CPU cooling
Arctic Freezer 7 + Arctic Silver 5
GPU cooling
Stock Heatsink and fan
OS
Windows Vista Ultimate 64
Monitor
Hanns-G 22" LCD
xtascox is offline   Reply With Quote
Old 02-19-09   #92 (permalink)
New to Overclock.net
 
Join Date: Feb 2009
Posts: 59

Rep: 4 MrDanny Unknown
Unique Rep: 13
Trader Rating: 0
Default

k info for beginner
MrDanny is offline   Reply With Quote
Old 03-12-09   #93 (permalink)
New to Overclock.net
 
intel nvidia

Join Date: Mar 2009
Posts: 5

Rep: 0 Djibrille Unknown
Unique Rep: 0
Trader Rating: 0
Default

I know I'm a little late but I was intrigued by the dictionary programming challenge after I did the brute force approach and found it not finishing after 1/2 hour on my aging AthlonXP laptop. So I changed it to use a HashMap and now it only takes about 8 secs to print the results. That surprised me to say the least.
Code:
import java.io.*;
import java.util.*;

public class Words{
	public static String sort(String word){
		char[] cwd=word.toCharArray();
		Arrays.sort(cwd);
		return new String(cwd);
	}
	public static void main (String args[]){
		if(args.length!=2){
	              System.out.println("Usage: java Words [filename_scrambled_words] [filename_wordlist]");
			System.exit(0);
		}
		File scrambled=new File(args[0]);
		File wordlist=new File(args[1]);
		if(!scrambled.isFile()){
			System.out.println(scrambled+" Not a file!");
			System.exit(0);                              
		}
		if(!wordlist.isFile()){
			System.out.println(wordlist+" Not a file!");
			System.exit(0);
			}                                                           
		try{
		BufferedReader brScram=new BufferedReader(new FileReader(scrambled));
		BufferedReader brWords=new BufferedReader(new FileReader(wordlist));
		String scrWord,goodWord;
		HashMap dictMap=new HashMap();
		while((goodWord=brWords.readLine())!=null){
			goodWord=goodWord.trim();
			String sortedGood=sort(goodWord);
			LinkedList el=new LinkedList();                                      
			if(dictMap.get(sortedGood)!=null)
				el=(LinkedList)(dictMap.get(sortedGood));
			el.add(goodWord);
			dictMap.put(sortedGood,el);
		}
		int count=0;                                                                             
		while((scrWord=brScram.readLine())!=null){
				scrWord=scrWord.trim();
				System.out.println("nSearching for: "+scrWord+"--n");
				System.out.println("      Found: "+dictMap.get(sort(scrWord))+"n");
				count++;
		}                                                                             
		System.out.println("     Searched : "+count+" scrambled words");
		brWords.close();
		brScram.close();
		}catch(Exception e){
			System.out.println("      Could not read file(s)!!! Exiting......"+e); 
			System.exit(0);
		}
	}
}
__________________
System: Custom built
CPU
Intel E8400@3,6ghz
Motherboard
Asus p5k-e WiFi-AP
Memory
OCZ 2x1gb 800mhz 5-5-5-15
Graphics Card
EVGA 8800gt
Hard Drive
Seagate 250gb+WD320gb
Sound Card
on-board
Power Supply
Corsair HX620
Case
Antec 3800
CPU cooling
Thermalright Ultra120 Extreme \nSchyte 120mm Fan
OS
Windows XP Pro SP3
Monitor
Samsung 226BW

Last edited by Djibrille : 03-12-09 at 03:41 PM
Djibrille is offline   Reply With Quote
Old 03-12-09   #94 (permalink)
New to Overclock.net
 
intel nvidia

Join Date: Mar 2009
Posts: 5

Rep: 0 Djibrille Unknown
Unique Rep: 0
Trader Rating: 0
Default

Final version of my java program takes ~6 secs to print the results(the ruby solution presented earlier here takes almost 13 secs on my sistem):
Code:
import java.io.*;
import java.util.*;

public class Words{
	public static String sort(String word){
		char[] cwd=word.toCharArray();
		Arrays.sort(cwd);
		return new String(cwd);
	}
	public static void main (String args[]){
		if(args.length!=2){
			System.out.println("Usage: java Words [filename_scrambled_words] [filename_wordlist]");
			System.exit(0);
		}
		File scrambled=new File(args[0]);
		File wordlist=new File(args[1]);
		if(!scrambled.isFile()){
			System.out.println(scrambled+" Not a file!");
			System.exit(0);                              
		}
		if(!wordlist.isFile()){
			System.out.println(wordlist+" Not a file!");
			System.exit(0);
			}                                                           
		try{
		BufferedReader brScram=new BufferedReader(new FileReader(scrambled));
		BufferedReader brWords=new BufferedReader(new FileReader(wordlist));
		String scrWord,goodWord;
		HashMap<String,LinkedList<String>> dictMap=new HashMap<String,LinkedList<String>>();
		while((goodWord=brWords.readLine())!=null){
			goodWord=goodWord.trim();
			String sortedGood=sort(goodWord);
			LinkedList<String> el=new LinkedList<String>();                                      
			if(dictMap.get(sortedGood)!=null)
				el=dictMap.get(sortedGood);
			el.add(goodWord);
			dictMap.put(sortedGood,el);
		}
		int count=0;                                                                             
		while((scrWord=brScram.readLine())!=null){
				scrWord=scrWord.trim();
				System.out.println("nSearching for: "+scrWord+"--n");
				System.out.println("      Found: "+dictMap.get(sort(scrWord))+"n");
				count++;
		}                                                                             
		System.out.println("     Searched : "+count+" scrambled words");
		brWords.close();
		brScram.close();
		}catch(Exception e){
			System.out.println("      Could not read file(s)!!! Exiting......"+e); 
			System.exit(0);
		}
	}
}
I would have liked to compare the speed with the c solution of rabidgnome's but I can't get it to compile with gcc on my linux OS(something about a blank character i'm too lasy to try and fix it as is quite hard to follow c code).
__________________
System: Custom built
CPU
Intel E8400@3,6ghz
Motherboard
Asus p5k-e WiFi-AP
Memory
OCZ 2x1gb 800mhz 5-5-5-15
Graphics Card
EVGA 8800gt
Hard Drive
Seagate 250gb+WD320gb
Sound Card
on-board
Power Supply
Corsair HX620
Case
Antec 3800
CPU cooling
Thermalright Ultra120 Extreme \nSchyte 120mm Fan
OS
Windows XP Pro SP3
Monitor
Samsung 226BW

Last edited by Djibrille : 03-12-09 at 04:08 PM
Djibrille is offline   Reply With Quote
Old 03-12-09   #95 (permalink)
With great difficulty
 
rabidgnome229's Avatar
 
intel nvidia

Join Date: Feb 2006
Location: Pittsburgh
Posts: 5,210

Rep: 614 rabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famous
Unique Rep: 370
FAQs Submitted: 6
Trader Rating: 5
Default

I tried copy/pasting my solution and I got the same thing, I think the forum is formatting something badly. Attached to this post is the code (rename to .c)

Never got around to doing that hash table
Attached Files
File Type: txt rabidgnome229.txt (5.1 KB, 14 views)
__________________
System: It goes to eleven
CPU
E6300
Motherboard
DS3
Memory
2GB XMS2 DDR2-800
Graphics Card
EVGA 8600GTS
Hard Drive
1.294 TB
Sound Card
Audigy 2 ZS
Power Supply
Corsair 520HX
Case
Lian-Li v1000B Plus
CPU cooling
TTBT
GPU cooling
Thermalright V2
OS
Arch Linux/XP
Monitor
Samsung 226bw
rabidgnome229 is offline Overclocked Account   Reply With Quote
Old 03-13-09   #96 (permalink)
New to Overclock.net
 
intel nvidia

Join Date: Mar 2009
Posts: 5

Rep: 0 Djibrille Unknown
Unique Rep: 0
Trader Rating: 0
Default

I just did some tests on my laptop.
Best results on my Java program: ~4.4 secs(this happens from second run onwards, os must be doing some caching, probably the dict file that's what takes the most time)
Best result on Rabid's c program:~1,5 secs(same thing here -- not the first run)
Wow, 3 times faster, i never beleived the difference would be so big. I could probably make it a little faster by removing some of the printing statements but I dont think it will ever be near as fast. That's something for the java developpers to think about, they claimed only 30% slower code comparing to C(maybe C++ I dont quite remember).
Anyway thanks rabid for the code! Nicely done!
__________________
System: Custom built
CPU
Intel E8400@3,6ghz
Motherboard
Asus p5k-e WiFi-AP
Memory
OCZ 2x1gb 800mhz 5-5-5-15
Graphics Card
EVGA 8800gt
Hard Drive
Seagate 250gb+WD320gb
Sound Card
on-board
Power Supply
Corsair HX620
Case
Antec 3800
CPU cooling
Thermalright Ultra120 Extreme \nSchyte 120mm Fan
OS
Windows XP Pro SP3
Monitor
Samsung 226BW
Djibrille is offline   Reply With Quote
Old 03-15-09   #97 (permalink)
Linux Lobbyist
 
GodofGrunts's Avatar
 
intel nvidia

Join Date: Nov 2007
Location: Hamilton, Ohio
Posts: 2,437

Rep: 140 GodofGrunts is acknowledged by manyGodofGrunts is acknowledged by many
Unique Rep: 116
Trader Rating: 2
Default

I always knew Java was slow, but 3x times? Wow, I'm glad I decided to dumb Java early.
__________________
Quote:
Originally Posted by MrDeodorant View Post
Oh, and the mayonnaise fanboys come out. Go back to your cave, troll. 2mm of mayonnaise is all anyone really needs. You don't have the ketchup bandwidth to support any more.

L2Sammich, noob. Besides, I'm running a Miracle Whip distro.
Click below to show/hide Hidden Text Below!
Quote:
Originally Posted by Raptor_Jesus View Post
Exactly what GodofGrunts said.
Quote:
Originally Posted by GodofGrunts View Post
Exactly what Raptor_Jesus said.
Quote:
Originally Posted by kerbitroy View Post
Exactly what they said :P
Quote:
Originally Posted by GodofGrunts View Post
Exactly what she said.
Quote:
Originally Posted by esocid View Post
Universe asplodes.





System: The Monolith
CPU
Q6600 G0 3.21 GHz
Motherboard
Asus P5K
Memory
4 GB 2x2GB OCZ
Graphics Card
8800GT Alpha Dog Edition 512MB
Hard Drive
WD 500GB Sata
Sound Card
Onboard
Power Supply
600W Ultra
Case
Monolith
CPU cooling
Scythe Ninja 2
GPU cooling
Aftermarket
OS
Mythbuntu 9.04 x64
Monitor
17" Dell LCD
GodofGrunts is offline GodofGrunts's Gallery   Reply With Quote
Old 03-15-09   #98 (permalink)
ATI Enthusiast
 
intel ati

Join Date: Dec 2008
Location: Rogers Park, 60626
Posts: 3,469

Rep: 406 Asus Mobile is a proven memberAsus Mobile is a proven memberAsus Mobile is a proven memberAsus Mobile is a proven memberAsus Mobile is a proven member
Unique Rep: 321
Trader Rating: 0
Default

Dude I ain't going to do your homework!
__________________
System: Asus F8VA-C1 Mobile Madman!
CPU
T9400 2.53Ghz OC 2.85Ghz
Motherboard
Asus
Memory
4GB PC2-6400 OC 900Mhz
Graphics Card
ATI Mobility Radeon HD 3650 OC 780/500
Hard Drive
WD 320GB @5400
Sound Card
Realtek
CPU cooling
Stock
GPU cooling
Stock
OS
Vista 32bit Home Premium
Monitor
14" WXGA+
Asus Mobile is offline   Reply With Quote
Old 03-15-09   #99 (permalink)
Linux Lobbyist
 
GodofGrunts's Avatar
 
intel nvidia

Join Date: Nov 2007
Location: Hamilton, Ohio
Posts: 2,437

Rep: 140 GodofGrunts is acknowledged by manyGodofGrunts is acknowledged by many
Unique Rep: 116
Trader Rating: 2
Default

Where did that come from?
__________________
Quote:
Originally Posted by MrDeodorant View Post
Oh, and the mayonnaise fanboys come out. Go back to your cave, troll. 2mm of mayonnaise is all anyone really needs. You don't have the ketchup bandwidth to support any more.

L2Sammich, noob. Besides, I'm running a Miracle Whip distro.
Click below to show/hide Hidden Text Below!
Quote:
Originally Posted by Raptor_Jesus View Post
Exactly what GodofGrunts said.
Quote:
Originally Posted by GodofGrunts View Post
Exactly what Raptor_Jesus said.
Quote:
Originally Posted by kerbitroy View Post
Exactly what they said :P
Quote:
Originally Posted by GodofGrunts View Post
Exactly what she said.
Quote:
Originally Posted by esocid View Post
Universe asplodes.





System: The Monolith
CPU
Q6600 G0 3.21 GHz
Motherboard
Asus P5K
Memory
4 GB 2x2GB OCZ
Graphics Card
8800GT Alpha Dog Edition 512MB
Hard Drive
WD 500GB Sata
Sound Card
Onboard
Power Supply
600W Ultra
Case
Monolith
CPU cooling
Scythe Ninja 2
GPU cooling
Aftermarket
OS
Mythbuntu 9.04 x64
Monitor
17" Dell LCD
GodofGrunts is offline GodofGrunts's Gallery   Reply With Quote
Old 03-18-09   #100 (permalink)
Case Modder
 
Spotswood's Avatar
 
Join Date: Jul 2008
Location: New Hampshire, USA
Posts: 235

Rep: 46 Spotswood is acknowledged by some
Unique Rep: 39
Trader Rating: 0
Default

Here's portions of my implementation of the "scrambled text" challenge. This C# implementation stores a dictionary of the list of scrambled words that have a given "weight." The large file test executes in NUnit in around ~1.3 seconds.

Code:
using System;
using System.Collections.Generic;
using System.Text;

namespace Scrambled
{
    public class ScrambledWord : IScrambledWord
    {
        private String scrambledLetters;    // As read from stream
        private int weight;                 // Summation of the character values in scrambledLetters

        public ScrambledWord(String scrambledLetters)
        {
            this.scrambledLetters = scrambledLetters;
            this.weight = CalculateWeight(scrambledLetters);
        }

        public bool LettersMatch(String text)
        {
            if (text.Length != scrambledLetters.Length) return false;
            for (int i = 0; i < scrambledLetters.Length; i++)
            {
                int index = text.IndexOf(scrambledLetters[i]);
                if (index == -1)
                    return false;
                text = text.Remove(index, 1);
            }
            return true;
        }

        public String Letters
        {
            get { return scrambledLetters; }
        }

        public int Weight
        {
            get { return weight; }
        }

        public static unsafe int CalculateWeight(String text)
        {
            int weight = 0;
            fixed (char* str = text)
            {
                for (int i = 0; i < text.Length; i++)
                    weight += str[i];
            }
            return weight;
        }

    }
}
Code:
using System;
using System.Collections.Generic;
using System.Text;

namespace Scrambled
{
    public class ScrambledWordList : IScrambledWordList
    {
        IDictionary<int /*weight*/, List<IScrambledWord> /*words with same weight*/> wordList;

        public ScrambledWordList()
        {
            wordList = new Dictionary<int, List<IScrambledWord>>();
        }

        public ScrambledWordList(IWordStream stream)
        {
            wordList = new Dictionary<int, List<IScrambledWord>>();
            String word;
            while ((word = stream.ReadWord()) != null)
                AddScrambledWord(new ScrambledWord(word));
        }

        public bool ContainsWord(String text)
        {
            int textWeight = ScrambledWord.CalculateWeight(text);
            List<IScrambledWord> list;
            if (wordList.TryGetValue(textWeight, out list) == false)
                return false;
            return AnyWordLettersMatch(list, text);
        }

        public void Add(IScrambledWord sw)
        {
            AddScrambledWord((ScrambledWord)sw);
        }

        void AddScrambledWord(ScrambledWord sw)
        {
            List<IScrambledWord> list;
            if (wordList.TryGetValue(sw.Weight, out list) == false)
            {
                list = new List<IScrambledWord>();
                wordList.Add(sw.Weight, list);
            }
            list.Add(sw);
        }

        bool AnyWordLettersMatch(List<IScrambledWord> list, String text)
        {
            int matches = 0;
            foreach (IScrambledWord sw in list)
            {
                // There could be many scrambled words 
                // that match the supplied text.
                if (sw.LettersMatch(text) == true)
                {
                    ++matches;
                    // This is wicked slow:
                    //Console.WriteLine(sw.Letters + " is " + text);
                }
            }
            return matches == 0 ? false : true;
        }
    }
}
Code:
// snip...

        [Test]
        public void ContainsWord_matches_in_huge_word_list()
        {
            IScrambledWordList scrambledList = 
                ScrambledWordListFactory.CreateWordList(
                    WordStreamFactory.CreateWordStream(GetManifestResourceStream("UnitTest.Resources.shuffle.txt")));
            IWordStream ws = 
                WordStreamFactory.CreateWordStream(GetManifestResourceStream("UnitTest.Resources.172K-words.txt"));
            String word;
            int count = 0;
            while ((word = ws.ReadWord()) != null)
                count += scrambledList.ContainsWord(word) ? 1 : 0;
            Assert.AreEqual(12877, count);
        }
__________________
Rich
Custom Wooden Case Builder
Overclock.net Mod of the Month

Last edited by Spotswood : 03-19-09 at 09:15 PM
Spotswood is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -5. The time now is 11:41 PM.


Overclock.net is a Carbon Neutral Site Creative Commons License

Terms of Service / Forum Rules | Privacy Policy | DMCA Info | Advertising | Become an Official Vendor
Copyright © 2009 Shogun Interactive Development. Most rights reserved.
Page generated in 0.17786 seconds with 9 queries