Coding Challenge #1

Anything and Everything about Uplink

Moderators: jelco, bert_the_turtle, Chris, Icepick, Rkiver

Adriac
level5
level5
Posts: 3504
Joined: Wed Jan 23, 2002 7:20 am

Postby Adriac » Sun Oct 24, 2004 9:25 pm

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


You can do anything in one line of Perl.

Though I assume you mean 80 characters and not one instruction :)
00010001000100000000101100010111000 10110000100010001100001011111000101 10000100100000111100010000000011010 0001011000111100001000100001011
Stewsburntmonkey
level5
level5
Posts: 11553
Joined: Wed Jul 10, 2002 7:44 pm
Location: Nashville, TN
Contact:

Postby Stewsburntmonkey » Sun Oct 24, 2004 9:49 pm

Well in many languages newlines are not needed, but I meant it could be done with a single statement (ie one semicolon).

I just ran my perl script for all numbers 1-8,000,000 and now have a 10.5 MB file of all the happy numbers in that range, fun! Actually its pretty useless, but whatever. :)
User avatar
LLamaBoy
level5
level5
Posts: 1627
Joined: Sun Aug 18, 2002 12:18 pm
Location: Cyprus
Contact:

Postby LLamaBoy » Sun Oct 24, 2004 9:55 pm

ODDin wrote:Oh boy, you did it in VB.net...
What makes it a little bit hard for me to get, from the depths of my VB6. But I think I got the general idea.

Only few things I totally can't get:
1) You can condition a definition of a variable? Neat.

2) What do the "New String" and "New Ineger" things do, exactly? I know that in VB.net even strings and integers are considered objects, but how does that work?

3) WHY, oh WHY did you use recursion?


1) Yes, in most language, variables can be defined anywhere, not only at the start of the code/procedure.

2) The "New Integer(num.Length - 1) {}" defines an array of Integers with the upper bound (index of last element) the length of the string minus one.
"New String", on the other hand calls the String class constructor to create a new string of a single digit, then it'd be converted to integer to be stored in the above-mentioned Integer array.

3) Why recursion? Because it's easier! I wanted to be first :P
Even though I got no bonus points for promptness :(
User avatar
Iris
level5
level5
Posts: 2423
Joined: Wed Apr 09, 2003 6:15 am
Location: Land of the Morning Calm

Postby Iris » Mon Oct 25, 2004 3:36 am

' quick & dirty solution in HotBasic console mode...
'

Code: Select all

$apptype console             
app.icon   = "iris.ico"         
defint tempval, ctr, itercnt
defstr number, tempnum, history

cls
input "Enter a number: "; number
tempnum = number
history = number + "."

do
  inc(itercnt)
  tempval = 0
  for ctr = 1 to tempnum.length
    if val(mid$(tempnum, ctr, 1) > 0 then
      tempval = tempval + val(mid$(tempnum, ctr, 1)^2
    end if
  next
  tempnum = str$(tempval)
  number  = tempnum + "."
  if instr(history, number) then
    print left$(history, instr(history, ".", 0)); " is not a happy number."
    pause
    application.terminate
  else
    history = history + number
  end if
  print "Iteration "; str$(itercnt); " = "; tempnum
loop until tempval = 1
print left$(history, instr(history, ".", 0)); " is a happy number!"
pause
end
[/code]
Image
User avatar
NeoThermic
Introversion Staff
Introversion Staff
Posts: 6256
Joined: Sat Mar 02, 2002 10:55 am
Location: ::1
Contact:

Re: Coding Challenge #1

Postby NeoThermic » Wed Oct 27, 2004 8:39 pm

Darksun wrote:.

I'm going to give a week for entries, but if you don't think that is long enough, tell me and I may extend it.

Good luck.


And? We are more than a week later buddy!

NeoThermic
Stewsburntmonkey
level5
level5
Posts: 11553
Joined: Wed Jul 10, 2002 7:44 pm
Location: Nashville, TN
Contact:

Postby Stewsburntmonkey » Wed Oct 27, 2004 9:14 pm

Darksun wrote: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.
theblisterexists
level2
level2
Posts: 127
Joined: Wed Jun 15, 2022 11:53 pm

Re: Coding Challenge #1

Postby theblisterexists » Wed Jun 29, 2022 2:26 pm

In fact, sometimes going through such coding challenges is really very useful, because often you will have to put a lot of effort to pass them. This will not only help you consolidate your existing knowledge, but you can also learn something new for sure. I regularly visit the TLe Apps site to get new ideas for coding challenges that I regularly do to make sure I've mastered the concepts I've learned in the programming language.
juliapaulson654
level0
Posts: 9
Joined: Fri Aug 20, 2021 11:12 am

Re:

Postby juliapaulson654 » Mon Aug 29, 2022 11:58 pm

Iris wrote:' quick & dirty solution in HotBasic console mode...
'

Code: Select all

$apptype console             
app.icon   = "iris.ico"         
defint tempval, ctr, itercnt
defstr number, tempnum, history

cls
input "Enter a number: "; number
tempnum = number
history = number + "."

do
  inc(itercnt)
  tempval = 0
  for ctr = 1 to tempnum.length
    if val(mid$(tempnum, ctr, 1) > 0 then
      tempval = tempval + val(mid$(tempnum, ctr, 1)^2
    end if
  next
  tempnum = str$(tempval)
  number  = tempnum + "."
  if instr(history, number) then
    print left$(history, instr(history, ".", 0)); " is not a happy number."
    pause
    application.terminate
  else
    history = history + number
  end if
  print "Iteration "; str$(itercnt); " = "; tempnum
loop until tempval = 1
print left$(history, instr(history, ".", 0)); " is a happy number!"
pause
end
[/code]



Sorry to say. This code is proper work in my compiler. Kindly help me with how I can execute this code. Kindly check my website there is a code issue or my website issue?
juliapaulson654
level0
Posts: 9
Joined: Fri Aug 20, 2021 11:12 am

Re:

Postby juliapaulson654 » Mon Aug 29, 2022 11:59 pm

Iris wrote:' quick & dirty solution in HotBasic console mode...
'

Code: Select all

$apptype console             
app.icon   = "iris.ico"         
defint tempval, ctr, itercnt
defstr number, tempnum, history

cls
input "Enter a number: "; number
tempnum = number
history = number + "."

do
  inc(itercnt)
  tempval = 0
  for ctr = 1 to tempnum.length
    if val(mid$(tempnum, ctr, 1) > 0 then
      tempval = tempval + val(mid$(tempnum, ctr, 1)^2
    end if
  next
  tempnum = str$(tempval)
  number  = tempnum + "."
  if instr(history, number) then
    print left$(history, instr(history, ".", 0)); " is not a happy number."
    pause
    application.terminate
  else
    history = history + number
  end if
  print "Iteration "; str$(itercnt); " = "; tempnum
loop until tempval = 1
print left$(history, instr(history, ".", 0)); " is a happy number!"
pause
end
[/code]



Sorry to say. This code is proper work in my compiler. Kindly help me with how I can execute this code. Kindly check my website there is a code issue or my website issue?
juliapaulson654
level0
Posts: 9
Joined: Fri Aug 20, 2021 11:12 am

Re:

Postby juliapaulson654 » Tue Aug 30, 2022 12:01 am

Iris wrote:' quick & dirty solution in HotBasic console mode...
'

Code: Select all

$apptype console             
app.icon   = "iris.ico"         
defint tempval, ctr, itercnt
defstr number, tempnum, history

cls
input "Enter a number: "; number
tempnum = number
history = number + "."

do
  inc(itercnt)
  tempval = 0
  for ctr = 1 to tempnum.length
    if val(mid$(tempnum, ctr, 1) > 0 then
      tempval = tempval + val(mid$(tempnum, ctr, 1)^2
    end if
  next
  tempnum = str$(tempval)
  number  = tempnum + "."
  if instr(history, number) then
    print left$(history, instr(history, ".", 0)); " is not a happy number."
    pause
    application.terminate
  else
    history = history + number
  end if
  print "Iteration "; str$(itercnt); " = "; tempnum
loop until tempval = 1
print left$(history, instr(history, ".", 0)); " is a happy number!"
pause
end
[/code]



Sorry to say. This code is proper work in my compiler. Kindly help me with how I can execute this code. Kindly check my website there is a code issue or my website issue?

Return to “General”

Who is online

Users browsing this forum: No registered users and 15 guests