Earn extra honor and gain new allies!
Honor is earned for each new codewarrior who joins.
Learn more
Code
Diff
  • def high_and_low(str_in):
        arr = sorted(int(n) for n in str_in.split(" "))
        return "{} {}".format(arr[-1], arr[0])
  • 1-highAndLow=lambda string:max(string.split(" "))+" "+min(string.split(" "))
    1+def high_and_low(str_in):
    2+ arr = sorted(int(n) for n in str_in.split(" "))
    3+ return "{} {}".format(arr[-1], arr[0])
Numbers
Algorithms
Code
Diff
  • isOdd = lambda n: ("False", "True")[n & 1]
  • 1-def isOdd(n):
    2- return ("False", "True")[n & 1]
    1+isOdd = lambda n: ("False", "True")[n & 1]
Numbers
Algorithms
Code
Diff
  • def isOdd(n):
        return ("False", "True")[n & 1]
  • 11 def isOdd(n):
    2- return str(bool(n & 1))
    2+ return ("False", "True")[n & 1]
Code
Diff
  • const deepThought =(a, b)=> a * b;
  • 1-function m (a, b) {
    2- a * b
    3-}
    1+const deepThought =(a, b)=> a * b;
Code
Diff
  • #define isLeap(y) !(y%4) * y%100 || !(y%400)
  • 1-#define isLeap(x) x % 4 == 0 && (x % 100 != 0 || x % 400 == 0)
    1+#define isLeap(y) !(y%4) * y%100 || !(y%400)

here ya go

Code
Diff
  • #include <stdlib.h>
    #include <string.h>
    
    char *fanis(const char *input, int value) {
      size_t n = strlen(input);
      char *output = (char *) malloc((n + 1) * sizeof(char));
      for(size_t i=0; i<n; i++) {
          output[i] = input[i] + value;
      }
      return output;
    }
  • 1-char * fanis (char * input, int value) {
    2- int length = strlen (input);
    3- char * output = (char *) malloc (sizeof(char) * (length + 1));
    4-
    5- memcpy (output, input, length + 1);
    6-
    1+#include <stdlib.h>
    2+#include <string.h>
    3+
    4+char *fanis(const char *input, int value) {
    5+ size_t n = strlen(input);
    6+ char *output = (char *) malloc((n + 1) * sizeof(char));
    7+ for(size_t i=0; i<n; i++) {
    8+ output[i] = input[i] + value;
    9+ }
    77 return output;
    88 }
    9-

may have a slight edge

Code
Diff
  • require 'benchmark'
    
    strings = []
    
    10000.times do
      string = Array.new(rand(500..1000)) do rand(10) end.join
      strings << string
    end
    
    def solution1(s) s.chars.sum(&:to_i) end
    def solution2(s) s.sum - s.size * 48 end
    def solution3(s) s.sum do |x| x - 48 end end
    def solution4(s) s.sum do |x| x - '0' end end
    
    Benchmark.bmbm do |x|
      x.report do strings.each(&method(:solution1)) end # farekkusu
      x.report do strings.each(&method(:solution2)) end # ttakuru88
      x.report do strings.each(&method(:solution3)) end # steffan153 (test code author)
      x.report do strings.each(&method(:solution4)) end # rowcased
    end
    
    puts "<PASSED::>" # prevent from saying failed tests
  • 77 strings << string
    88 end
    99
    1010 def solution1(s) s.chars.sum(&:to_i) end
    1111 def solution2(s) s.sum - s.size * 48 end
    1212 def solution3(s) s.sum do |x| x - 48 end end
    13+def solution4(s) s.sum do |x| x - '0' end end
    1313
    1414 Benchmark.bmbm do |x|
    1515 x.report do strings.each(&method(:solution1)) end # farekkusu
    1616 x.report do strings.each(&method(:solution2)) end # ttakuru88
    17- x.report do strings.each(&method(:solution3)) end # steffan153 (me)
    18+ x.report do strings.each(&method(:solution3)) end # steffan153 (test code author)
    19+ x.report do strings.each(&method(:solution4)) end # rowcased
    1818 end
    1919
    2020 puts "<PASSED::>" # prevent from saying failed tests
Code
Diff
  • #include <numeric>
    using namespace std;
    
    int verify_sum(string a, string b)
    {
      return a.size() * !b.size()
             || accumulate(a.begin(), a.end(), 0)
             == accumulate(b.begin(), b.end(), 0);
    }
  • 11 #include <numeric>
    2-#include <string>
    2+using namespace std;
    33
    4-bool verify_sum(std::string a, std::string b) {
    5- return a.length() == 0 || b.length() == 0 || std::accumulate(a.begin(), a.end(), 0) == std::accumulate(b.begin(), b.end(), 0);
    4+int verify_sum(string a, string b)
    5+{
    6+ return a.size() * !b.size()
    7+ || accumulate(a.begin(), a.end(), 0)
    8+ == accumulate(b.begin(), b.end(), 0);
    66 }
Code
Diff
  • #include <string.h>
    #include <stdlib.h>
    
    char *Hi(void) {
        char *ans = (char*) malloc(13 * sizeof(char));
        strcpy(ans, "Hello World.");
        return ans;
    }
  • 1-char* Hi (void)
    2-{char* ans;
    3-asprintf(&ans, "Hello World.");
    4-return ans;}
    1+#include <string.h>
    2+#include <stdlib.h>
    3+
    4+char *Hi(void) {
    5+ char *ans = (char*) malloc(13 * sizeof(char));
    6+ strcpy(ans, "Hello World.");
    7+ return ans;
    8+}
Code
Diff
  • fizzBuzz=lambda n:'Fizz'*(n%3==0)+'Buzz'*(n%5==0)or str(n)
  • 1-def fizzBuzz(num):
    2- return 'Fizz' *(not num % 3) + 'Buzz' *(not num % 5) or str(num)
    1+fizzBuzz=lambda n:'Fizz'*(n%3==0)+'Buzz'*(n%5==0)or str(n)
Code
Diff
  • public class BiggerNum
    {
      public static int compare(int a, int b)
      {
        return Math.max(a, b);
      }
    }
  • 1-public class BiggerNum{
    2-
    3- public static int compare(int a, int b) {
    4- return a>b ? a : b;
    1+public class BiggerNum
    2+{
    3+ public static int compare(int a, int b)
    4+ {
    5+ return Math.max(a, b);
    55 }
    6-
    77 }
Code
Diff
  • kata=lambda x:x[0]=='a'
  • 1-kata=lambda x:x.startswith("a")
    1+kata=lambda x:x[0]=='a'
Code
Diff
  • function invert(fore) {
      var back = "";
      for(var i=fore.length-1; i>=0; i--) {
        back += fore[i];
      }
      return back;
    }
  • 1-const invert = str => str.replace(/./g, (_,o,s) => s.charAt(s.length-1-o));
    1+function invert(fore) {
    2+ var back = "";
    3+ for(var i=fore.length-1; i>=0; i--) {
    4+ back += fore[i];
    5+ }
    6+ return back;
    7+}
Arrays
Algorithms
Data
Code
Diff
  • function pairs(arr) {
      var arr_set = new Set(arr);
      for(var item of arr_set) {
        if(arr_set.has(item * -1)) {      
          return item < 0 ? [item, item * -1] : [item * -1, item]
        }
      }
    }
  • 11 function pairs(arr) {
    22 var arr_set = new Set(arr);
    3- for(var item of new Set(arr)) {
    3+ for(var item of arr_set) {
    44 if(arr_set.has(item * -1)) {
    55 return item < 0 ? [item, item * -1] : [item * -1, item]
    66 }
    77 }
    88 }
Loading more items...