Code Challenge: "Hello, World"

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

34 Replies - 9378 Views - Last Post: 16 June 2014 - 06:15 AM

#1 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Code Challenge: "Hello, World"

Posted 27 November 2012 - 05:04 PM

A fairly simple challenge: Display "Hello, World" n times where 0 <= n <= 1000.
Rules: No loops (for, foreach, do/while,while are all forbidden, including their parallel equivalents), no LINQ. Each display must be on a separate line.

It's ok if your routine can handle values larger than 1000, just note any limitations.

Note: I have two solutions to this, just looking to see if there are any interesting methods I've missed :)

Is This A Good Question/Topic? 0
  • +

Replies To: Code Challenge: "Hello, World"

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5417
  • View blog
  • Posts: 11,613
  • Joined: 02-June 10

Re: Code Challenge: "Hello, World"

Posted 27 November 2012 - 10:38 PM

Recurrsion isn't a loop, right?

Spoiler

Was This Post Helpful? 4
  • +
  • -

#3 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Code Challenge: "Hello, World"

Posted 27 November 2012 - 11:46 PM

Recursion is fine, and is one of the ways I'd come up with.

Another method:
Spoiler

Was This Post Helpful? 2
  • +
  • -

#4 wiero  Icon User is offline

  • D.I.C Head

Reputation: 48
  • View blog
  • Posts: 78
  • Joined: 29-June 11

Re: Code Challenge: "Hello, World"

Posted 28 November 2012 - 12:41 AM

*
POPULAR

Spoiler

This post has been edited by wiero: 28 November 2012 - 12:42 AM

Was This Post Helpful? 5
  • +
  • -

#5 rnty  Icon User is offline

  • D.I.C Head

Reputation: 20
  • View blog
  • Posts: 145
  • Joined: 14-August 12

Re: Code Challenge: "Hello, World"

Posted 28 November 2012 - 02:47 AM

@Momerath:Why did you incorporate the if?
Was This Post Helpful? 0
  • +
  • -

#6 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 349
  • View blog
  • Posts: 1,491
  • Joined: 08-April 09

Re: Code Challenge: "Hello, World"

Posted 28 November 2012 - 04:13 AM

If is a condition not a loop if i remember correctly

This post has been edited by Michael26: 28 November 2012 - 04:13 AM

Was This Post Helpful? 0
  • +
  • -

#7 rnty  Icon User is offline

  • D.I.C Head

Reputation: 20
  • View blog
  • Posts: 145
  • Joined: 14-August 12

Re: Code Challenge: "Hello, World"

Posted 28 November 2012 - 04:46 AM

I don't mean to challenge the author.I want to understand the logic.

This post has been edited by rnty: 28 November 2012 - 04:46 AM

Was This Post Helpful? 0
  • +
  • -

#8 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Code Challenge: "Hello, World"

Posted 28 November 2012 - 07:26 AM

The if statements allow you to specify any number from 0 to 1023 and it will print that number of "Hello, World" statements.

And I'd not thought of wiero's method, even though I've used it before to answer someones question. I've also thought of a few more ways, but I'll let others post things before I add to this :)
Was This Post Helpful? 0
  • +
  • -

#9 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1365
  • View blog
  • Posts: 3,006
  • Joined: 05-April 11

Re: Code Challenge: "Hello, World"

Posted 30 November 2012 - 12:58 PM

First time I have got to create a struct :D/> !
Spoiler

This post has been edited by CasiOo: 30 November 2012 - 12:58 PM

Was This Post Helpful? 0
  • +
  • -

#10 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2233
  • View blog
  • Posts: 9,401
  • Joined: 29-May 08

Re: Code Challenge: "Hello, World"

Posted 30 November 2012 - 02:44 PM

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DIC_HelloWorld
{
  class Program
  {
    static void Main(string[] args)
    {
      int n=10;
      Console.WriteLine(Repeat("Hello World\n",n));
    }
    static string Repeat(string text, int n,string output="")
    {
      return (new Func<String>[]{()=> output,()=>Repeat(text,n-1,output + text)})[Math.Sign(n)]();
     }
  }
}



Would this be allowed.

  {
      int n=10;
      Enumerable.Repeat("Hello World!", n).ToList().ForEach(Console.WriteLine);
    }
 


  {
      int n=10;
      Console.WriteLine(String.Concat(Enumerable.Repeat("Hello World!\n", n)));
    }
 


This post has been edited by AdamSpeight2008: 30 November 2012 - 02:50 PM

Was This Post Helpful? 0
  • +
  • -

#11 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7531
  • View blog
  • Posts: 12,607
  • Joined: 19-March 11

Re: Code Challenge: "Hello, World"

Posted 30 November 2012 - 03:05 PM

View PostMomerath, on 28 November 2012 - 01:46 AM, said:

Recursion is fine, and is one of the ways I'd come up with.

Another method:
Spoiler



This is a glorious kludge.
Was This Post Helpful? 0
  • +
  • -

#12 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4408
  • View blog
  • Posts: 7,653
  • Joined: 08-June 10

Re: Code Challenge: "Hello, World"

Posted 04 December 2012 - 08:26 AM

I'm surprised this didn't come up yet:

Spoiler


Simulates a loop, but doesn't use any actual loop constructs, so I think it fits the challenge.
Was This Post Helpful? 0
  • +
  • -

#13 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: Code Challenge: "Hello, World"

Posted 06 December 2012 - 03:02 PM

I'm surprised also:
Spoiler

This post has been edited by lucky3: 06 December 2012 - 03:03 PM

Was This Post Helpful? 2
  • +
  • -

#14 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: Code Challenge: "Hello, World"

Posted 07 December 2012 - 01:40 AM

Would this (and similar) be considered like recursion?

Spoiler

Was This Post Helpful? 0
  • +
  • -

#15 snypeNET  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 06-October 12

Re: Code Challenge: "Hello, World"

Posted 09 January 2013 - 11:39 AM

My professors would probably go nuts if they saw this code because I was told to never use this method. But this was the first solution I could think of when I read the restrictions of the challenge.

Spoiler


View Postjon.kiparsky, on 30 November 2012 - 03:05 PM, said:

View PostMomerath, on 28 November 2012 - 01:46 AM, said:

Recursion is fine, and is one of the ways I'd come up with.

Another method:
Spoiler



This is a glorious kludge.

I agree.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3