Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,158,980 members, 7,838,493 topics. Date: Thursday, 23 May 2024 at 11:35 PM

Programming Challenge - Programming (2) - Nairaland

Nairaland Forum / Science/Technology / Programming / Programming Challenge (3620 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)

Re: Programming Challenge by csharpjava(m): 9:31am On Sep 23, 2012
@omo_to_dun
Sorry I wasn't aware of the rules before.

Updated
Now 14 lines with C# to make it compile and run online, you can try it online at: http://london.gs.aster.arvixe.com/default.aspx
Question: How many lines will yours take on the server side that will allow us to try it online?

using System;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{protected void Page_Load(object sender, EventArgs e){}
protected void Button1_Click(object sender, EventArgs e)
{ 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);}}}

omo_to_dun:
Your code cannot compile as is! Javanian's code can be compiled and run. Mine can simply be pasted onto a Python CLI and it will run. You have used ojoro. And btw, it only runs for that particular string.

Re: Programming Challenge by PrinceNN(m): 8:52pm On Sep 23, 2012
Ok so here is my own...done in python and took 7 lines.

http://pastebin.com/ABFH4nSM
Re: Programming Challenge by PrinceNN(m): 8:58pm On Sep 23, 2012
@javania i Also tried it in java , took 7 lines too....same approach i used in python

http://pastebin.com/LBdChWkr
Re: Programming Challenge by Nobody: 9:34pm On Sep 23, 2012
Re: Programming Challenge by xcitedjay(m): 10:30pm On Sep 23, 2012
₱®ÌИСΞ:
@javania i Also tried it in java , took 7 lines too....same approach i used in python

http://pastebin.com/LBdChWkr

Nice one. I like the way you replaced the '0' with 'No' inside the output parameter.
Re: Programming Challenge by PrinceNN(m): 1:35am On Sep 24, 2012
xcitedjay:

Nice one. I like the way you replaced the '0' with 'No' inside the output parameter.
Thnx mate..... Figured its shorter that way
Re: Programming Challenge by PrinceNN(m): 1:51am On Sep 24, 2012
omo_to_dun: @OP, so it looks like I'm the winner (with four lines).
a, s = [0] * 128, "The quick brown Fox jumps over the lazy Dog".lower()
for c in s: a[ord(c)] = a[ord(c)] + 1

for i in range(97,123): print("{0} {1}'s".format('No' if a[i] == 0 else a[i], chr(i)))



Wow.... magnificent...tho u used 3 lines .... But its nt yet over.

http://pastebin.com/j2KdVzCS
Re: Programming Challenge by Nobody: 5:11am On Sep 24, 2012
Re: Programming Challenge by PrinceNN(m): 7:27am On Sep 24, 2012
Lol...thnx mate....your method used less characters tho so I'd giv it to you...bravo
Re: Programming Challenge by ektbear: 7:31am On Sep 24, 2012
What is the point of optimizing down to X lines if the approach works correctly and doesn't have any major unnecessary steps?
Re: Programming Challenge by PrinceNN(m): 7:54am On Sep 24, 2012
ekt_bear: What is the point of optimizing down to X lines if the approach works correctly and doesn't have any major unnecessary steps?
The fewer the lines d more complex d algorithm because u hav to achieve as much as possible with each line. And dat is actually what the challenge was
Re: Programming Challenge by lordZOUGA(m): 9:31am On Sep 24, 2012
₱®ÌИСΞ:

The fewer the lines d more complex d algorithm because u hav to achieve as much as possible with each line. And dat is actually what the challenge was
not necessarily true... I think you are confusing "syntax" for "algorithm"... That I used
while(i < 8 )
{//code}
or used
for(i = 0; i < 8 ; )
{//code}

does not change the fact that I used a loop in my program or algorithm and probably does not speed up or slow down the program in any way
Re: Programming Challenge by lordZOUGA(m): 9:43am On Sep 24, 2012
and this is a biased challenge... I can never write a code that short in C++.... 6 lines... Mostly
Re: Programming Challenge by PrinceNN(m): 11:49am On Sep 24, 2012
lordZOUGA:
not necessarily true... I think you are confusing "syntax" for "algorithm"... That I used
while(i < 8 )
{//code}
or used
for(i = 0; i < 8 ; )
{//code}

does not change the fact that I used a loop in my program or algorithm and probably does not speed up or slow down the program in any way

True but I also stated earlier that it tests both the algorithm and how easy or complex it is to achieve d same result with various languages and their syntax

And for the c++ etc issue I count only the lines in the main methods
Re: Programming Challenge by csharpjava(m): 12:19pm On Sep 24, 2012
₱®ÌИСΞ:

And for the c++ etc issue I count only the lines in the main methods

@omo_to_dun
In that case the code I posted before has only 6 lines and anyone can try it online at http://london.gs.aster.arvixe.com/default.aspx The execution time shown makes me feel this code might be the fastest cheesy.

{ 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 csharpjava(m): 4:12pm On Sep 24, 2012
Is the lack of response to the C# .NET code I posted similar to a case of: A guy walking into a bar wearing a Microsoft C# .NET T-shirt and challenges any Java, Python etc loving pansy to fight. and The place gets deathly quiet, and one guy in the back of the room slides his chair back from a table, stands up, and glares at the intruder. grin
Re: Programming Challenge by Nobody: 4:47pm On Sep 24, 2012
Re: Programming Challenge by PrinceNN(m): 4:49pm On Sep 24, 2012
Hahaha chill...I'm replying with my phone now...when I arrive my destination I will compare d speed. Let's see if mr c# leaves d bar in a wheelchair
Re: Programming Challenge by csharpjava(m): 7:02pm On Sep 24, 2012
omo_to_dun:
Chei, see as you yab us finish!

grin grin grin grin
Re: Programming Challenge by csharpjava(m): 7:12pm On Sep 24, 2012
₱®ÌИСΞ:
Hahaha chill...I'm replying with my phone now...when I arrive my destination I will compare d speed. Let's see if mr c# leaves d bar in a wheelchair

Oya make we see if no be the Java, Python etc loving guys go leave the bar with a black eye grin !
Re: Programming Challenge by mkwayisi: 3:14am On Sep 26, 2012
Well this is getting interesting. I have my code too in C on a single line. Now don't tell me about readability, you asked for it wink Perhaps you can write a shorter one with no lines at all wink
int main(int i,char**v){if(i>1){int c[26];char *p=v[1];for(i=0;i<26winkc[i++]=0;while(*p!='\0'){if(*p>=97&&*p<=122)*p-=32;if(*p>=65&&*p<=90)c[*p-65]++;p++;}for(i=0;i<26;i++)if(c[i]>0)printf("%cs=%d\n",i+65,c[i]);}return 0;}

After compiling, launch the exe passing the sentence as an argument. A screenshot is shown below.

Re: Programming Challenge by Javanian: 7:37am On Sep 26, 2012
mkwayisi: Well this is getting interesting. I have my code too in C on a single line. Now don't tell me about readability, you asked for it wink Perhaps you can write a shorter one with no lines at all wink
int main(int i,char**v){if(i>1){int c[26];char *p=v[1];for(i=0;i<26winkc[i++]=0;while(*p!='\0'){if(*p>=97&&*p<=122)*p-=32;if(*p>=65&&*p<=90)c[*p-65]++;p++;}for(i=0;i<26;i++)if(c[i]>0)printf("%cs=%d\n",i+65,c[i]);}return 0;}

After compiling, launch the exe passing the sentence as an argument. A screenshot is shown below.

u don high?
Re: Programming Challenge by lordZOUGA(m): 7:46am On Sep 26, 2012
mkwayisi: Well this is getting interesting. I have my code too in C on a single line. Now don't tell me about readability, you asked for it wink Perhaps you can write a shorter one with no lines at all wink
int main(int i,char**v){if(i>1){int c[26];char *p=v[1];for(i=0;i<26winkc[i++]=0;while(*p!='\0'){if(*p>=97&&*p<=122)*p-=32;if(*p>=65&&*p<=90)c[*p-65]++;p++;}for(i=0;i<26;i++)if(c[i]>0)printf("%cs=%d\n",i+65,c[i]);}return 0;}

After compiling, launch the exe passing the sentence as an argument. A screenshot is shown below.
hey, that's sleazy!!! Sleazier than what omo_to_dun did with ASCII. In my books, your code is 10 lines. You might consider using recursion to eliminate one loop and using inline if statements like
(x == y) ? Cout << y: cout << x;
Re: Programming Challenge by mkwayisi: 12:32pm On Sep 26, 2012
@lordZouga: Did you just call the C code "sleazy"? Well, guess what? You ain't seen "sleazy" yet!
Perl. 1 line. 71 total characters. BTW, I don't call this "sleazy", rather "nifty". Got it?
$c{$_}++for(split/ */,lc$ARGV[0]);print$c{$_}||'No'," $_\'s\n"for(a..z)
To test it, pass the sentence as an argument to the script. Eg:
$> perl test.pl "The Quick Brown Fox Jumps Over The Lazy Dog"
Write any shorter and I can't compete!

1 Like

Re: Programming Challenge by Javanian: 11:01am On Sep 27, 2012
Can some start a thread with a new challenge...i learnt alot from this one...
Re: Programming Challenge by ektbear: 12:05pm On Sep 27, 2012
Re: Programming Challenge by lordZOUGA(m): 1:32pm On Sep 27, 2012
ekt_bear: Ask and ye shall receive:
https://www.nairaland.com/1059733/another-programming-challenge
I knew you can't help it...
Re: Programming Challenge by sholyboy89: 1:36pm On Sep 27, 2012
I enjoyed reading some of the solutions here, howver the most efficient solutions are the one that runs in constant time by creating an integer array representing the decimal equivalents of ascii values in the alphabet i.e 0-127 and initialize all 128 elemetns to 0. Loop through the sentence in 0(N) time and everytime you encountered a letter l. access the L value in the array as array(l) and then increment. Some languages would convert the l to the decimal ascii equivalent if not you can easily do this yourself, time complexity for the conversion is trivial. You could use an hash table but that is overkill considering you only need space for 128 elements. The number of lines of code have nothing to do with the efficiency of the program. I think the solutions written in C would probably run the fastest being native and all
Re: Programming Challenge by PrinceNN(m): 1:45pm On Sep 27, 2012
mkwayisi: Well this is getting interesting. I have my code too in C on a single line. Now don't tell me about readability, you asked for it wink Perhaps you can write a shorter one with no lines at all wink
int main(int i,char**v){if(i>1){int c[26];char *p=v[1];for(i=0;i<26winkc[i++]=0;while(*p!='\0'){if(*p>=97&&*p<=122)*p-=32;if(*p>=65&&*p<=90)c[*p-65]++;p++;}for(i=0;i<26;i++)if(c[i]>0)printf("%cs=%d\n",i+65,c[i]);}return 0;}

After compiling, launch the exe passing the sentence as an argument. A screenshot is shown below.

this is not allowed nau...you just crammed everything into one line...
Re: Programming Challenge by PrinceNN(m): 1:58pm On Sep 27, 2012
mkwayisi: @lordZouga: Did you just call the C code "sleazy"? Well, guess what? You ain't seen "sleazy" yet!
Perl. 1 line. 71 total characters. BTW, I don't call this "sleazy", rather "nifty". Got it?
$c{$_}++for(split/ */,lc$ARGV[0]);print$c{$_}||'No'," $_\'s\n"for(a..z)
To test it, pass the sentence as an argument to the script. Eg:
$> perl test.pl "The Quick Brown Fox Jumps Over The Lazy Dog"
Write any shorter and I can't compete!

nice code...shortest so far, but you arent rilly supposed to pass any arguements to the program. the rules demanded that you include the default text in the code.
Re: Programming Challenge by jacob05(m): 10:32am On Sep 28, 2012
php 2 lines

you can save it in an index file =>index.php

and
query it with the string
index.php?string=The Quick Brown Fox Jumps Over The Lazy Dog

list($alph,$search) = array("abcdefghijklmnopqrstuvwxyz",$_GET['string']);
for($i=0;$i<strlen($alph);$i++) printf("%s %s's <br />",preg_match_all('/'.$alph{$i}.'/',$search,$match),$alph{$i});
Re: Programming Challenge by jacob05(m): 10:36am On Sep 28, 2012
To Make the Challenge interesting EVERY semi-colon {except in loops and other constructs} should attract a new line .... i.e the C code pasted earlier

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

Come Here To Learn Java In Pidgin / Tried To Hire A Programmer In Lagos Today He Told Me $100 A Day Or Forget / Free Online Embedded System Design Tutorials with PIC

(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. 40
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.