Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,148,627 members, 7,801,813 topics. Date: Friday, 19 April 2024 at 12:03 AM

Programming Challenge - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Programming Challenge (3597 Views)

Programming Challenge For Beginners Competition Two N20000 -SEASON 2- / Programming Challenge For Beginners N20000 / Facebook Programming Challenge Question (2) (3) (4)

(1) (2) (3) (Reply) (Go Down)

Programming Challenge by PrinceNN(m): 11:46am On Sep 22, 2012
Write code in any language dat processes a sentence(text only) and prints out the occurence of each letter of the alphabet in the sentence using the format

3 a's
2 b's
....
No z's

Default Text = "The quick brown Fox jumps over the lazy Dog"

Its sounds easy but I'm looking for the 'smallest' 'fool-proof' code
Re: Programming Challenge by lordZOUGA(m): 12:15pm On Sep 22, 2012
₱®ÌИСΞ:
Write code in any language dat processes a sentence(text only) and prints out the occurence of each letter of the alphabet in the sentence using the format

3 a's
2 b's
....
No z's

Default Text = "The quick brown Fox jumps over the lazy Dog"

Its sounds easy but I'm looking for the 'smallest' 'fool-proof' code

I would use a dynamically allocated memory... No need allocating memory for a 'z' when it might not be there...
Re: Programming Challenge by PrinceNN(m): 1:39pm On Sep 22, 2012
lordZOUGA:
I would use a dynamically allocated memory... No need allocating memory for a 'z' when it might not be there...
Pls could u shed more light on dat?
Re: Programming Challenge by Javanian: 2:45pm On Sep 22, 2012
I did this in a hurry its quite lengthy but it works

http://pastebin.ca/2206694

I'll see if i can write a shorter version...
Re: Programming Challenge by PrinceNN(m): 3:26pm On Sep 22, 2012
Javanian: I did this in a hurry its quite lengthy but it works

http://pastebin.ca/2206694

I'll see if i can write a shorter version...


gr8 effort...thnx... smiley
tho the main challenge is getting d job done in fewer lines of code
User input isn't rilly required cus I supplied a default text to process.....
Re: Programming Challenge by PrinceNN(m): 3:30pm On Sep 22, 2012
Javanian has 142 lines...anybody?
Re: Programming Challenge by Javanian: 3:33pm On Sep 22, 2012
Thanks but its actually 135 lines grin dont count the space..
Re: Programming Challenge by Javanian: 3:35pm On Sep 22, 2012
and don't count it like that, i can easily remove 30 lines without actually adding or subtracting any code... grin
Re: Programming Challenge by PrinceNN(m): 3:42pm On Sep 22, 2012
ok remove as much as u can.... The floor is still open smiley
Re: Programming Challenge by Javanian: 4:02pm On Sep 22, 2012
http://pastebin.ca/2206725

The same code in 66 lines grin
Re: Programming Challenge by Otuabaroku: 5:34pm On Sep 22, 2012
Hi Javanian i was wondering the need for you to declare array of chars. Anyway, here is my solution:

public class CharCounter{


String sentence="The quick brown Fox jumps over the lazy Dog";
int as,bs,cs,ds,es,fs,gs,hs,is,js,ks,ls,ms,ns,os,ps,qs,rs,ss,ts,us,vs,ws,xs,ys,zs;

public void countChar()
{
char[] Text = sentence.toLowerCase().toCharArray();
for (char text:Text)
switch(text)
{
case 'a':
as++;
break;
case 'b':
bs++;
break;
case 'c':
cs++;
break;

case 'd':
ds++;
break;

case 'e':
es++;
break;


case 'f':
fs++;
break;


case 'g':
gs++;
break;

case 'h':
hs++;
break;

case 'i':
is++;
break;
case 'j':
js++;
break;

case 'k':
ks++;
break;

case 'l':
ls++;
break;

case 'm':
ms++;
break;

case 'n':
ns++;
break;

case 'o':
os++;
break;

case 'p':
ps++;
break;


case 'q':
qs++;
break;




case 'r':
rs++;
break;

case 's':
ss++;
break;

case 't':
ts++;
break;

case 'u':
us++;
break;

case 'v':
vs++;
break;

case 'w':
ws++;
break;

case 'x':
xs++;
break;

case 'y':
ys++;
break;

case 'z':
zs++;
break;
}

}

public void printNoofOccuerrence()

{

System.out.println("There are " +as+" a's" );
System.out.println("There are " +bs+" b's" );
System.out.println("There are " +cs+" c's" );
System.out.println("There are " +ds+" d's" );
System.out.println("There are " +es+" e's" );
System.out.println("There are " +fs+" f's" );
System.out.println("There are " +gs+" g's" );
System.out.println("There are " +hs+" h's" );
System.out.println("There are " +is+" i's" );
System.out.println("There are " +js+" j's" );
System.out.println("There are " +ks+" k's" );
System.out.println("There are " +ls+" l's" );
System.out.println("There are " +ms+" m's" );
System.out.println("There are " +ns+" n's" );
System.out.println("There are " +os+" o's" );
System.out.println("There are " +qs+" q's" );
System.out.println("There are " +rs+" r's" );
System.out.println("There are " +ss+" s's" );
System.out.println("There are " +us+" u's" );
System.out.println("There are " +vs+" v's" );
System.out.println("There are " +ws+" w's" );
System.out.println("There are " +xs+" x's" );
System.out.println("There are " +ys+" y's" );
System.out.println("There are " +zs+" z's" );
}
public static void main(String[] args)
{

CharCounter cc = new CharCounter();
cc.countChar();
cc.printNoofOccuerrence();
}
}
Re: Programming Challenge by naijaswag1: 5:56pm On Sep 22, 2012
Javanian: http://pastebin.ca/2206725

The same code in 66 lines grin
Javanian: Thanks but its actually 135 lines grin dont count the space..

The number of lines of your code does not determine how efficient it is.I will rather code for shorter running time than for shorter lines of code.
Re: Programming Challenge by Javanian: 6:01pm On Sep 22, 2012
@otuabaroku I used a char array to compare it with the letter of the alphabets...

By the way, i will sue you for plagiarism grin...you wrote another version of my code grin...dont you know that code has a patent grin...we will see in court!...lol...just kidding...You did a great job...
Re: Programming Challenge by Javanian: 6:09pm On Sep 22, 2012
naija_swag:

The number of lines of your code does not determine how efficient it is.I will rather code for shorter running time than for shorter lines of code.

i know that but he was more interested in the number of lines so i wanted to prove to him that i can get it done in fewer lines...tongue
Re: Programming Challenge by Otuabaroku: 6:34pm On Sep 22, 2012
Javanian: @otuabaroku I used a char array to compare it with the letter of the alphabets...

By the way, i will sue you for plagiarism grin...you wrote another version of my code grin...dont you know that code has a patent grin...we will see in court!...lol...just kidding...You did a great job...
Oh I see. My bad, I would have stated "all rights reversed to Javanian". Forgive me I will not do it again. grin
Re: Programming Challenge by PrinceNN(m): 7:06pm On Sep 22, 2012
naija_swag:

The number of lines of your code does not determine how efficient it is.I will rather code for shorter running time than for shorter lines of code.

The shorter the code the more complex the algorithm. and besides fewer code pays off when issues like size or readability is an issue
i need an answer with far less lines because it displays how skilled the programmer is with algorithm or and how easy or complex it is to implement such in various programming languages. i had implemented this in python before asking and it occupied far less lines...
Re: Programming Challenge by PrinceNN(m): 7:10pm On Sep 22, 2012
floor is still open...@otuabaroku and @javanian gr8 job but still too much lines sha grin
Re: Programming Challenge by Javanian: 7:37pm On Sep 22, 2012
₱®ÌИСΞ:

. i had implemented this in python before asking and it occupied far less lines...
doing this in a language like python or PHP will take far more less lines..
Re: Programming Challenge by Nobody: 8:54pm On Sep 22, 2012
Re: Programming Challenge by Nobody: 9:01pm On Sep 22, 2012
Re: Programming Challenge by Javanian: 9:10pm On Sep 22, 2012
^ I will like to see it...so you still dey this forum?
Re: Programming Challenge by PrinceNN(m): 9:28pm On Sep 22, 2012
omo_to_dun: Python: 15 lines

def word(s):
a = [0] * 26
s = s.lower()
for c in s:
i = ord(c) - 97
if i > -1 and i < 26:
a[i] = a[i] + 1
return a
def emit(a):
for i in xrange(26):
if a[i] == 0:
print("No {0}'s".format(chr(i + 97)))
else:
print("{0} {1}'s".format(a[i], chr(i + 97)))
emit(word("The quick brown Fox jumps over the lazy Dog"))

Wow...magnificent....bravo....shortest so far... but. 15 lines? Knock out a few and u hav it .... Floor is still open sha...smiley
Re: Programming Challenge by Nobody: 10:51pm On Sep 22, 2012
Re: Programming Challenge by Nobody: 10:52pm On Sep 22, 2012
Re: Programming Challenge by Nobody: 10:59pm On Sep 22, 2012
Re: Programming Challenge by Javanian: 11:28pm On Sep 22, 2012
http://pastebin.ca/2206823

15 lines...this is the best i can go...
Re: Programming Challenge by PrinceNN(m): 11:48pm On Sep 22, 2012
omo_to_dun: Ok, this lecturer is boring me. Here we go. Python: 9 lines

def word(s):
a, s = [0] * 26, s.lower()
for c in s:
i = ord(c) - 97
if i > -1 and i < 26: a[i] = a[i] + 1
for i in xrange(26):
if a[i] == 0: print("No {0}'s".format(chr(i + 97)))
else: print("{0} {1}'s".format(a[i], chr(i + 97)))
word("The quick brown Fox jumps over the lazy Dog")



Javanian: I'll do yours later


Cool......but...still knock out a lil...I noticed u used ascii method...nice approach
Re: Programming Challenge by PrinceNN(m): 11:52pm On Sep 22, 2012
Javanian: http://pastebin.ca/2206823

15 lines...this is the best i can go...

Nice work..I am also goin 2 try 2 implement it in java...who knows, might be shorter
Re: Programming Challenge by PrinceNN(m): 11:56pm On Sep 22, 2012
Floor iz wide open ...
Re: Programming Challenge by csharpjava(m): 2:06am On Sep 23, 2012
Just six lines with C#:

{ string sentence = TextBox1.Text;
var sentenceQuery = from sentenceIn in sentence.ToLower().ToCharArray()
group sentenceIn by sentenceIn into m
select new { Key = m.Key, Count = m.Count() };
foreach (var item in sentenceQuery)
{Label1.Text += string.Format("Character: {0} Appears {1} time/s <br />", item.Key.ToString(), item.Count);}}}
Re: Programming Challenge by Nobody: 4:22am On Sep 23, 2012
Re: Programming Challenge by ektbear: 6:17am On Sep 23, 2012
Just loop through the characters, store in a hash (with the hash having entries by default 0 for keys that don't exist). Then output the hash.

Something like this:

(1) (2) (3) (Reply)

Tried To Hire A Programmer In Lagos Today He Told Me $100 A Day Or Forget / Please Don't Learn To Code / .

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 46
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.