Coding Challenge #1

Anything and Everything about Uplink

Moderators: jelco, bert_the_turtle, Chris, Icepick, Rkiver

camman977
level2
level2
Posts: 85
Joined: Wed Sep 29, 2004 10:45 pm
Location: Michigan, U.S.A.
Contact:

Postby camman977 » Thu Oct 21, 2004 1:21 am

Hope this helps.

it would help a lot if i could understand it...lol....i have only ever made simple programs so i don't understand anything you just said
Image
User avatar
Iris
level5
level5
Posts: 2423
Joined: Wed Apr 09, 2003 6:15 am
Location: Land of the Morning Calm

Postby Iris » Thu Oct 21, 2004 1:46 am

camman977 wrote:
Hope this helps.

it would help a lot if i could understand it...lol....i have only ever made simple programs so i don't understand anything you just said


Well, if that didn't help, maybe this will:

1. Try to imagine solving the problem in your head. If this is hard to do, look at how this problem is solved manually. Use Google to see 'happy numbers' then understand how it was solved by hand. If you cannot visualize the solution in your mind, you cannot translate that into code.

2. Try to write down a simple pseudo-code (simple math formulas mixed with basic programming functions like IF..THEN, etc.) to put down that solution on paper.

3. Start replacing the rest of your solution on paper with actual programming structures like DO..LOOP, FOR..TO..NEXT..., etc., until you get a working model.

The trick there is to try to break the problem one by one, ie. first, how do i separate a number into its digits (oh! i can change it into a string so i can play around with it! i can't do that with actual numeric values), second, how do i square each digit and add them all together (well, i can use MID$() to extract each digit, convert it to a number using VAL(), square it using ^2, then add it to a number variable), etc. etc.

I suggest you practice putting your solutions on paper before actually coding it, or use diagrams or a flowchart so you can 'visualize' what needs to be done to get the problem solved.

(wow, this is like deja vu, back in my high school days when i used to tutor my classmates.......how time flies.........)
Image
ODDin
level5
level5
Posts: 2521
Joined: Fri Jul 04, 2003 10:44 pm
Location: Haifa, Israel
Contact:

Postby ODDin » Thu Oct 21, 2004 6:36 pm

Actually, you can get all the sperate digits out of an integer as well. It'll just be a bit harder to do.
Stewsburntmonkey
level5
level5
Posts: 11553
Joined: Wed Jul 10, 2002 7:44 pm
Location: Nashville, TN
Contact:

Postby Stewsburntmonkey » Thu Oct 21, 2004 8:44 pm

It is funny since you can do the whole sum of squares in one line of Perl. :)
User avatar
Iris
level5
level5
Posts: 2423
Joined: Wed Apr 09, 2003 6:15 am
Location: Land of the Morning Calm

Postby Iris » Fri Oct 22, 2004 1:59 am

Stewsburntmonkey wrote:It is funny since you can do the whole sum of squares in one line of Perl. :)


Well, not everybody knows Perl.
Image
Stewsburntmonkey
level5
level5
Posts: 11553
Joined: Wed Jul 10, 2002 7:44 pm
Location: Nashville, TN
Contact:

Postby Stewsburntmonkey » Fri Oct 22, 2004 2:02 am

Well, who's fault it that? :P

I do think people should understand that some languages are better for certain tasks than others. Languages have their niches and I think it is a good idea to know and understand those. I know it makes my life a lot easier. :)
User avatar
Iris
level5
level5
Posts: 2423
Joined: Wed Apr 09, 2003 6:15 am
Location: Land of the Morning Calm

Postby Iris » Fri Oct 22, 2004 2:12 am

Stewsburntmonkey wrote:Well, who's fault it that? :P

I do think people should understand that some languages are better for certain tasks than others. Languages have their niches and I think it is a good idea to know and understand those. I know it makes my life a lot easier. :)


Certainly not the guy who knows Perl but not VB, and vice versa.

I also think people should know that, and still bear in mind that it's the basic, even if the language you're using is not the best, that matters the most. It is quite easy for someone like you to do the whole sum of squares in one line of Perl, because you've been good at it for quite some time and from my perception, your depth on the language is almost like knowing the back of of your hand because you spent time and effort learning the basics. So I say to camman977, know the basics, the language(s) come second, they're just tools later on that you can play with.
Image
Stewsburntmonkey
level5
level5
Posts: 11553
Joined: Wed Jul 10, 2002 7:44 pm
Location: Nashville, TN
Contact:

Postby Stewsburntmonkey » Fri Oct 22, 2004 2:25 am

Yea, once you know the basics it is pretty easy to pickup whatever language you need. Just last summer I needed to do some work with Excel spreadsheets and so I learned VB since it has the best interface with Excel spreadsheets. Since I knew the basics of programming it wasn't too diffecult to pickup VB. Basic isn't a bad place to start learning programming, that's where I started at least. :)
einstein
level5
level5
Posts: 1463
Joined: Mon Mar 04, 2002 5:23 pm
Location: Scotland

Postby einstein » Fri Oct 22, 2004 12:10 pm

my programming/scripting ability is very very weak...


If i need to do something i'll learn how to do it then and there... my general knowledge of the language is therefore terrible...

Perfect example being by knowledge of PHP. I can happily make a website which relies solely on PHP, and it's interface with mySQL (the downlink challenge being an example)... but in order to do this challenge i had to basically "learn" the php functions required by reading php.net and then applying them...

I am not, nor do i claim to be, a programmer/scripter but when it is required that i code/script something... i can answer then call! :D
User avatar
Iris
level5
level5
Posts: 2423
Joined: Wed Apr 09, 2003 6:15 am
Location: Land of the Morning Calm

Postby Iris » Fri Oct 22, 2004 1:09 pm

einstein wrote:my programming/scripting ability is very very weak...


If i need to do something i'll learn how to do it then and there... my general knowledge of the language is therefore terrible...

Perfect example being by knowledge of PHP. I can happily make a website which relies solely on PHP, and it's interface with mySQL (the downlink challenge being an example)... but in order to do this challenge i had to basically "learn" the php functions required by reading php.net and then applying them...

I am not, nor do i claim to be, a programmer/scripter but when it is required that i code/script something... i can answer then call! :D


So I can categorize you then as "resourceful". =)
Image
coolsi
level5
level5
Posts: 3990
Joined: Wed Apr 10, 2002 6:46 pm

Postby coolsi » Fri Oct 22, 2004 8:34 pm

Finished my Happy Number eggdrop script! irc.gamesurge.net, #happynumbers if you want to play!
Nakatomi is coming
einstein
level5
level5
Posts: 1463
Joined: Mon Mar 04, 2002 5:23 pm
Location: Scotland

Postby einstein » Fri Oct 22, 2004 10:20 pm

lol coolsi very nice...

he even ignores you when you try and trick him!

hehe
Darksun
level5
level5
Posts: 6461
Joined: Sat Dec 07, 2002 7:08 pm
Location: 127.0.0.1

Postby Darksun » Sat Oct 23, 2004 6:34 pm

OK, I have the results.

Neothermic, Blasted Heath, Iris, coolsi, Stewsburntmonkey and Fartan and einstein all produced a solution, and get 1 point. Llamaboy was busy, and entered two different solutions (one in VB and the other in Java) so he gets 2 points.

Now, bonus points -

I liked coolsi for his unique approach - an IRC bot. That earns 1 bonus points.

I thought Stewsburntmonkey's solution (written in perl) was very neat, so that earns 3 bonus points

But the solution that impressed me most was one by Fartan (an old forumer who never posted much, but is apparantly still around), written in PureBasic ASM, for which he earns the grand prize of 5 bonus points.

Everyone, give yourself a pat on the back. I'll be posting another (harder!) challenge soon. I'm not posting the solutions - I'll leave that up to entrants. If you really want to see how someone solved the challenge, email them and ask them nicely, I'm sure they'll send you a copy of their code.
coolsi
level5
level5
Posts: 3990
Joined: Wed Apr 10, 2002 6:46 pm

Postby coolsi » Sat Oct 23, 2004 6:44 pm

Code: Select all

# happy.tcl - Calculates whether a number is happy or not
# By coolsi (coolsi@crazie.co.uk), 2004
#
# !happy <number>

bind pub - !happy happy

# [squares] will take the digits of a number, square them and sum them. How kind!
proc squares { numchars number } {
  set square 0
  for { set i 0 } { $i < $numchars } { incr i } {
    set num [string index $number $i]
    set square [expr [expr $square + [expr $num * $num]]]
  }
  return $square
}

# happy is called by !happy...original, huh?
proc happy { nick host hand chan text } {
  set number [lindex $text 0]
  if { $number < 1 } {
    putquick "PRIVMSG $chan ::|"
  } else {
    set numchars [string length $number]
    set squared [squares $numchars $number]

    while { $squared != 1 } {
      set squared [squares [string length $squared] $squared]
      if { $squared == 4 } { break }
    }
 
    if { $squared == 1 } {
      putquick "PRIVMSG $chan :$number is a happy number!"
    } elseif { $squared == 4 } {
      putquick "PRIVMSG $chan :$number is an unhappy number :("
    }
  }
}
Nakatomi is coming
Blasted heath
level5
level5
Posts: 1292
Joined: Sat Jul 12, 2003 2:29 pm
Location: Scotland ©
Contact:

Postby Blasted heath » Sat Oct 23, 2004 7:27 pm

My code, try not to wince too much

Code: Select all

<?PHP
$randomoruser = $_POST['randusr'];
$setoverall = 100;                  // Set a limit, in case of error and infinite loops
$overalltries = $setoverall;
if($randomoruser == "random"){
$intro = "Random number chosen";
$randint = rand(1,10000);              //Generate a random number between 1 and 10000
} elseif($randomoruser == "user"){
$randint = $_POST['submitnumber'];
$intro = "User chosen number";
} else {
echo "
<form action='' method='post'>
<input type='hidden' value='random' name='randusr'>Generate random number: <input type='submit' value='Click'>
</form>
<br />
<form action='' method='post'>
<input type='hidden' value='user' name='randusr'><input type='text' value='Insert own number' name='submitnumber'><input type='submit' value='Click'>
</form>
";
exit;
}
echo "<b>$intro:</b> $randint<br />";
for($overalltries = $setoverall; $overalltries > 0; $overalltries--){
$lengthint = strlen($randint);                                          //get random integer length
$pool = 0;
$runtimesone = 0;
echo "Trying number: $randint - ";
for($runtimesone == 0; $runtimesone < $lengthint; $runtimesone++){
$secondaryrun = $runtimesone + 1;
$dgts['$runtimesone'] = substr($randint, $runtimesone, 1);  // Put each character into $dgts array
echo "(" . $dgts['$runtimesone'] . ") ";
if($dgts['$runtimesone'] == 0){
$dgts['$runtimesone'] = "0";
}
$squared = $dgts['$runtimesone'] * $dgts['$runtimesone'];             // Square each digit
$pool = $pool + $squared;           // Add the squared numbers
}
echo "--- Sqaured and added = $pool<br />";
if(($pool == 1) || ($pool == 4) || ($pool == 0)){
if($pool == 4){
$floop = 1;
} else {
$floop = "invalid";
}
break;                                          // Halt the loop if it adds up to 1, or a 4 loop
}

$randint = $pool;
}
if($pool == 1){
echo "<br />We got ourselves a happy number!";
} else {
if($floop == 1){
echo "<br />Unhappy '4-loop' detected, not a happy number";
} elseif($floop == "invalid") {
echo "<br />Number supplied was 0, or not a number";
}
}
?>
Blasted heath

Return to “General”

Who is online

Users browsing this forum: No registered users and 19 guests