Code Challenge: "Hello, World"

• (3 Pages)
• 1
• 2
• 3

41 Replies - 20130 Views - Last Post: 06 February 2015 - 07:24 AM

#1 Momerath

• D.I.C Lover

Reputation: 1018
• Posts: 2,461
• 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

• Not here as much anymore

Reputation: 5972
• Posts: 13,055
• Joined: 02-June 10

Re: Code Challenge: "Hello, World"

Posted 27 November 2012 - 10:38 PM

Recurrsion isn't a loop, right?

Spoiler

#3 Momerath

• D.I.C Lover

Reputation: 1018
• Posts: 2,461
• 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

#4 wiero

Reputation: 49
• 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

#5 rnty

Reputation: 19
• Posts: 168
• Joined: 14-August 12

Re: Code Challenge: "Hello, World"

Posted 28 November 2012 - 02:47 AM

@Momerath:Why did you incorporate the if?

#6 Michael26

Reputation: 378
• Posts: 1,566
• 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

#7 rnty

Reputation: 19
• Posts: 168
• 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

#8 Momerath

• D.I.C Lover

Reputation: 1018
• Posts: 2,461
• 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

#9 CasiOo

• D.I.C Lover

Reputation: 1490
• Posts: 3,316
• 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 /> !
Spoiler

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

• MrCupOfT

Reputation: 2290
• Posts: 9,528
• 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;

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

#11 jon.kiparsky

• Pancakes!

Reputation: 8563
• Posts: 14,802
• Joined: 19-March 11

Re: Code Challenge: "Hello, World"

Posted 30 November 2012 - 03:05 PM

Momerath, 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.

#12 Curtis Rutland

• （╯°□°）╯︵ (~ .o.)~

Reputation: 4727
• Posts: 8,381
• 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.

#13 lucky3

• Friend lucky3 As IHelpable

Reputation: 233
• Posts: 770
• 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

#14 lucky3

• Friend lucky3 As IHelpable

Reputation: 233
• Posts: 770
• 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

#15 snypeNET

Reputation: 1
• 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

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

Momerath, 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.