1
\$\begingroup\$

For the last day of the semester, I decided to have some fun with my students and I showed them Code Golf... poor guys, they're addicted now.

The goal was to compete against the other students to solve those problems with what would be very usual rules for you. They had to use C# (because it's the language they are learning right now) so please do the same.

The length of each method must be the shortest including the method signature!

Tasks

Write functions that do the following:

  1. Input: string, int.
    Output: string.
    Description: the output string repeats the input string as many times as the input int.
    Example: a("Hi", 5) returns "HiHiHiHiHi".
  2. Input: int.
    Output: int.
    Description: the output is the quantity of prime numbers lower or equal to the input.
    Example: b(10) returns 4. b(100) returns 25.
  3. Input: object[].
    Output: object[].
    Description: returns the input without the null values.
    Example: c(new object[] { "Hello", 42, null, new Program(), null, null, false }) returns an array containing [Hello, 42, Program instance, false].
  4. Input: int.
    Output: int.
    Description: the output is the sum of all the integers from 1 to int input.
    Example: d(4) returns 10 (because 1 + 2 + 3 + 4 = 10).
  5. Input: char.
    Output: char.
    Description: the output is the input char with the other case. non-letters don't need to be checked.
    Example: e('a') returns 'A'. e('A') returns 'a'. e('-') may have unexpected results.
  6. Input: string, int.
    Output: bool.
    Description: true if and only if the length of the input string is a multiple of the input int.
    Example: f("Programmer", 5) returns true. f("Programming", 5) returns false.
  7. Input: string.
    Output: boolean.
    Description: true if and only if the input string is a palindrome.
    Example: g("level") returns true. g('label') returns false.

Their solutions

I took the best of each task that they presented to me.

  1. 64
  2. 135
  3. 105
  4. 30
  5. 71
  6. 45
  7. 83

Total: 533

(I will write their complete answers when the challenge is over).

Your rules

  • Use C# to be evaluated equally to my students.
  • Write each task on a separate line with the method signature (string a(string s,int i)), it is part of the answer.
  • Write your best score for each task so we know what score matches what task.
  • Write the sum of all task scores in the title of your answer.
  • The shortest overall score wins, but honorary mention may be awarded to the lowest score of a given task.

Note: It is not impossible that many of you have the same answer, my students did without cheating. Please consider that someont who has the same answer as you may simply have taken the same path and did not steal your idea.


FAQ:

  • Why should people here use C#? It's not the best language for code golfing !
    I know, but I would like to have some examples from pros like you to show them what a real golfer would do in their shoes.
\$\endgroup\$

closed as off-topic by Martin Ender Apr 20 '16 at 15:06

  • This question does not appear to be about programming puzzles or code golf within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    \$\begingroup\$ I think assigning your students golfing tasks is a nice idea but restricting this to C# may garner you some disapproval here :/ Perhaps encourage C# but don't require it? Just a heads up. \$\endgroup\$ – Calvin's Hobbies Apr 20 '16 at 15:03
  • 2
    \$\begingroup\$ Challenges with multiple unrelated parts are deemed off-topic. Please consider posting them as separate challenges. You can use the leaderboard from this challenge to automatically compute scores over all parts. But note that some of your individual parts are likely duplicates (which is exactly why multi-part challenges are disallowed). \$\endgroup\$ – Martin Ender Apr 20 '16 at 15:05
  • 3
    \$\begingroup\$ I'm voting to close this question as off-topic because this challenge consists of multiple unrelated subtasks. \$\endgroup\$ – Martin Ender Apr 20 '16 at 15:06
  • 3
    \$\begingroup\$ Just an idea, if you choose to pursue this question, I think posting each of your students' best answers individually as a tips question would probably be the best way to go about what you want. That's just my opinion though, if you want to ask you could try meta or chat. \$\endgroup\$ – FryAmTheEggman Apr 20 '16 at 15:18
  • 1
    \$\begingroup\$ Got 294 Yeee - Oh, post closed... \$\endgroup\$ – a-ctor Jun 12 '16 at 21:56
2
\$\begingroup\$

Score: 387

  1. string a(string s,int n){return string.Join(s,new string[n+1]);} (64)
  2. int b(int n){bool p=true;for(int i=2;i<n;i++)if(n%i<1)p=false;return n<2?0:b(n-1)+(p?1:0);} (91)
  3. object[]c(object[]l){return l.Where(x=>x!=null).ToArray();} (59)
  4. int d(int n){return(n+1)*n/2;} (30)
  5. char e(char c){return(char)(c<97?c+32:c-32);} (45)
  6. bool f(string s,int n){return s.Length%n<1;} (44)
  7. bool g(string s){return s.SequenceEqual(s.Reverse());} (54)

Total: 387

\$\endgroup\$
  • 1
    \$\begingroup\$ !0 is shorter than true. same for !1 shorter than false \$\endgroup\$ – Ven Apr 20 '16 at 14:50
  • \$\begingroup\$ Not with C#, thanks anyway. \$\endgroup\$ – SteeveDroz Apr 20 '16 at 14:52
  • \$\begingroup\$ @Oltarus 1>0 and 0>1 though... \$\endgroup\$ – Martin Ender Apr 20 '16 at 15:03
  • 1
    \$\begingroup\$ For part 5, c^32 should do. \$\endgroup\$ – Martin Ender Apr 20 '16 at 15:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.