Make a better score than my students!

1

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.

SteeveDroz

Posted 2016-04-20T14:46:37.287

Reputation: 2 399

Question was closed 2016-04-20T15:06:12.813

2

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.

– Calvin's Hobbies – 2016-04-20T15:03:16.367

Thanks for the advice. I'm still considering, because someone will probably find a Pyth code that does all the 7 tasks in 5 characters total ;-). I'll add a comment in the question. – SteeveDroz – 2016-04-20T15:05:33.603

2Challenges 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). – Martin Ender – 2016-04-20T15:05:39.110

3

I'm voting to close this question as off-topic because this challenge consists of multiple unrelated subtasks.

– Martin Ender – 2016-04-20T15:06:12.813

3

Just an idea, if you choose to pursue this question, I think posting each of your students' best answers individually as a [tag: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.

– FryAmTheEggman – 2016-04-20T15:18:26.893

1Got 294 Yeee - Oh, post closed... – a-ctor – 2016-06-12T21:56:41.713

1Got even better - 238 it is – a-ctor – 2016-06-13T16:52:38.967

Answers

2

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

SteeveDroz

Posted 2016-04-20T14:46:37.287

Reputation: 2 399

1!0 is shorter than true. same for !1 shorter than false – Ven – 2016-04-20T14:50:23.273

Not with C#, thanks anyway. – SteeveDroz – 2016-04-20T14:52:55.410

@Oltarus 1>0 and 0>1 though... – Martin Ender – 2016-04-20T15:03:21.487

1For part 5, c^32 should do. – Martin Ender – 2016-04-20T15:07:40.877