Ad
Code
Diff
  • def flip_the_number(x):
        return int(str(x)[::-1])
    • # Takes int or string input
    • # Returns int
    • def flip_the_number(x):
    • return int(str(x) if len(str(x)) <= 1 else str(flip_the_number(str(x)[1:])) + str(x)[0])
    • return int(str(x)[::-1])
Code
Diff
  • import string
    
    def rubegoldberg():
        alpha = string.ascii_lowercase
        find, total = "codewars", 1
        for x, y in enumerate(find):
            if x % 2 == 0:
                total += alpha.index(y)
            else:
                total -= alpha.index(y)
        total -= len(find)
        return total
    • import string
    • def rubegoldberg():
    • return 2-1
    • alpha = string.ascii_lowercase
    • find, total = "codewars", 1
    • for x, y in enumerate(find):
    • if x % 2 == 0:
    • total += alpha.index(y)
    • else:
    • total -= alpha.index(y)
    • total -= len(find)
    • return total
Code
Diff
  • palindrome=lambda s:s.lower()==s.lower()[::-1]
    • def palindrome(s): return s.lower() == s.lower()[::-1]
    • palindrome=lambda s:s.lower()==s.lower()[::-1]
Functions
Control Flow
Basic Language Features
Fundamentals

Added test cases

Code
Diff
  • def find_short(string):
        return len(sorted(string.split(" "),key=len)[0])
    • import java.util.stream.*;
    • public class Kata {
    • public static int findShort(String s) {
    • return Stream.of(s.split(" "))
    • .mapToInt(String::length)
    • .min()
    • .getAsInt();
    • }
    • }
    • def find_short(string):
    • return len(sorted(string.split(" "),key=len)[0])

Python Intepretation

Code
Diff
  • import string
    
    def HeLlOwOrLddddd(strng):
        strng = [x for x in strng if x in string.ascii_letters]
        return "".join([strng[x].upper() if x % 2 == 0 else strng[x].lower() for x in range(len(strng))])
    • public class HeLlOwOrLddddd {
    • public static String convert(String input) {
    • String salida = "";
    • boolean mayus = true;
    • for (int i=0;i<input.length();i++){
    • if (Character.isLetter(input.charAt(i))){
    • if (mayus){
    • salida+=Character.toUpperCase(input.charAt(i));
    • mayus=false;
    • }else{
    • salida+=Character.toLowerCase(input.charAt(i));
    • mayus=true;
    • }
    • }
    • }
    • return salida;
    • import string
    • }
    • }
    • def HeLlOwOrLddddd(strng):
    • strng = [x for x in strng if x in string.ascii_letters]
    • return "".join([strng[x].upper() if x % 2 == 0 else strng[x].lower() for x in range(len(strng))])
Code
Diff
  • def areMetaStrings(str1, str2):
        return sorted(str1.lower()) == sorted(str2.lower())
    • func areMetaStrings(_ str1: String, _ str2: String) -> Bool {
    • return str1.lowercased().sorted() == str2.lowercased().sorted()
    • }
    • def areMetaStrings(str1, str2):
    • return sorted(str1.lower()) == sorted(str2.lower())
Code
Diff
  • def rot13(s):
        rot = "abcdefghijklmnopqrstuvwxyzabcdefghijklm"
        return "".join([rot[rot.index(x)+13] for x in s])
    • def rot13(s):
    • return rot.encode(s,"rot_13")
    • rot = "abcdefghijklmnopqrstuvwxyzabcdefghijklm"
    • return "".join([rot[rot.index(x)+13] for x in s])
Code
Diff
  • def calculator(a, operator, b):
        operations = {"+": lambda x, y: x + y, "-": lambda x, y: x - y,
        "*": lambda x, y: x * y, "/": lambda x, y: x / y}
        return operations[operator](a, b)
    • func calculator(_ int1: Int, _ operation: String, _ int2: Int) {
    • //Code Here
    • }
    • def calculator(a, operator, b):
    • operations = {"+": lambda x, y: x + y, "-": lambda x, y: x - y,
    • "*": lambda x, y: x * y, "/": lambda x, y: x / y}
    • return operations[operator](a, b)
Code
Diff
  • def middle_string(string):
        return string[1:-1] if len(string) >= 3 else ""
    • func substringWithoutFirstAndLastElement(_ string: String) -> String {
    • guard string.count > 2 else { return "" }
    • return String(String(string.dropFirst()).dropLast())
    • }
    • def middle_string(string):
    • return string[1:-1] if len(string) >= 3 else ""
Code
Diff
  • def divisibility_by_3(x):
        return str(x/3).endswith(".0")
    • def divisibility_by_3(x):
    • x = abs(x)
    • while x > 9:
    • list_of_values = [int(d) for d in str(x)]
    • x = sum(list_of_values)
    • return x in [0, 3, 6, 9]
    • return str(x/3).endswith(".0")
Code
Diff
  • import math
    
    def circle_area(r):
        return round(math.pi*r*r, 2)
    
    def circle_cir(r):
        return round(math.pi*r*2, 2)
    • function CircleArea(r){
    • return Number((Math.PI*r*r).toFixed(2));
    • }
    • import math
    • function CircleCir(r){
    • return Number((Math.PI*r*2).toFixed(2));
    • }
    • def circle_area(r):
    • return round(math.pi*r*r, 2)
    • def circle_cir(r):
    • return round(math.pi*r*2, 2)
Code
Diff
  • highAndLow=lambda string:max(string.split(" "))+" "+min(string.split(" "))
    • import java.util.Arrays;
    • import java.util.stream.Collectors;
    • import java.util.List;
    • public class Kumite {
    • public static String highAndLow(String numbers) {
    • List<Integer> list = Arrays.stream(numbers.split(" "))
    • .map(Integer::valueOf)
    • .collect(Collectors.toList());
    • return list.stream().max(Integer::compareTo).get() + " " + list.stream().min(Integer::compareTo).get();
    • }
    • }
    • highAndLow=lambda string:max(string.split(" "))+" "+min(string.split(" "))
Code
Diff
  • def rotateLeft(array):
        return [[x[-i-1] for x in array] for i in range(len(array[0]))]
    
    def rotateRight(array):
        return [[x[i] for x in array][::-1] for i in range(len(array[0]))]
    • const rotateLeft = (arr) => {
    • let retArr = [[],[],[]]
    • for (let i = 0; i < arr.length; i++){
    • for (let j = 0; j < arr[i].length; j++){
    • retArr[i][j] = arr[j][arr.length - (i + 1)]
    • }
    • }
    • return retArr
    • }
    • def rotateLeft(array):
    • return [[x[-i-1] for x in array] for i in range(len(array[0]))]
    • const rotateRight = (arr) => {
    • let retArr = [[],[],[]]
    • for (let i = 0; i < arr.length; i++){
    • for (let j = 0; j < arr[i].length; j++){
    • retArr[i][j] = arr[arr.length - (j + 1)][i]
    • }
    • }
    • return retArr
    • }
    • def rotateRight(array):
    • return [[x[i] for x in array][::-1] for i in range(len(array[0]))]

This translation assumes that the first day of each month is a Sunday.

Code
Diff
  • def day_finder(day, month, year):
        week = ["Sunday", "Monday",  "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
        dates = {"Jan":[31, 0], "Feb":[28, 31], "Mar":[31, 59], "Apr":[30, 90],
        "May":[31, 120], "Jun":[30, 151], "Jul":[31, 181], "Aug":[31, 212],
        "Sep":[30, 243], "Oct":[31, 273], "Nov":[30, 304], "Dec":[31, 334]}
        full_months = {"Jan":"January", "Feb":"February", "Mar":"March", "Apr":"April",
        "May":"May", "Jun":"June", "Jul":"July", "Aug":"August", "Sep":"September", 
        "Oct":"October", "Nov":"November", "Dec":"December"}
        def week_day(num):
            inc = -1
            for x in range(num):
                inc += 1
                if inc == 7:
                    inc = 0
            return week[inc]
        if month not in dates:
            return "Invalid Date"
        if [year % x for x in [4, 100, 40]] == [0, 0, 0]:
            dates["Feb"][0] = 29
            for x in dates:
                if x != "Jan":
                    dates[x][1] += 1
        if month in dates:
            if day >= 1 and day <= dates[month][0]:
                total_day = dates[month][1] + day
                day_type = week_day(day)
                total_mark = None
                mark = None
                if str(day).endswith("1") and str(day) != "11":
                    mark = "st"
                elif str(day).endswith("2") and str(day) != "12":
                    mark = "nd"
                elif str(day).endswith("3") and str(day) != "13":
                    mark = "rd"
                else:
                    mark = "th"
                if str(total_day).endswith("1") and str(day) != "11":
                    total_mark = "st"
                elif str(total_day).endswith("2") and str(day) != "12":
                    total_mark = "nd"
                elif str(total_day).endswith("3") and str(day) != "13":
                    total_mark = "rd"
                else:
                    total_mark = "th"
                return f"The {day}{mark} of {full_months[month]} is a {day_type} and the {total_day}{total_mark} day of {year}."
        return "Invalid Date"
    • import java.io.*;
    • class Date
    • {
    • public static void main()throws IOException
    • {
    • BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
    • System.out.print("Enter the Date:(DD MM YYYY): ");
    • String k=in.readLine();
    • int d=Integer.parseInt(k.substring(0,2)), c,m=Integer.parseInt(k.substring(3,5)), y=Integer.parseInt(k.substring(6)), i, a1[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
    • String a2[]={"Sunday","Monday","Tuesday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
    • if((y%4==0 && y%100!=0)||y%400==0)
    • a1[2]=29;
    • if(m<1 || m>12 || d<1 || d>a1[m])
    • System.out.println("Invalid Date..............");
    • else
    • {
    • System.out.println("Valid Date.............");
    • for(i=1;i<m;i++)
    • d+=a1[i];
    • c=d;
    • for(i=1;i<y;i++)
    • if((i%4==0 && i%100!=0)||i%400==0)
    • d+=366;
    • else
    • d+=365;
    • System.out.println("The day value is: "+c+" days and thr day is "+a2[d%7]);
    • }
    • }
    • }
    • def day_finder(day, month, year):
    • week = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
    • dates = {"Jan":[31, 0], "Feb":[28, 31], "Mar":[31, 59], "Apr":[30, 90],
    • "May":[31, 120], "Jun":[30, 151], "Jul":[31, 181], "Aug":[31, 212],
    • "Sep":[30, 243], "Oct":[31, 273], "Nov":[30, 304], "Dec":[31, 334]}
    • full_months = {"Jan":"January", "Feb":"February", "Mar":"March", "Apr":"April",
    • "May":"May", "Jun":"June", "Jul":"July", "Aug":"August", "Sep":"September",
    • "Oct":"October", "Nov":"November", "Dec":"December"}
    • def week_day(num):
    • inc = -1
    • for x in range(num):
    • inc += 1
    • if inc == 7:
    • inc = 0
    • return week[inc]
    • if month not in dates:
    • return "Invalid Date"
    • if [year % x for x in [4, 100, 40]] == [0, 0, 0]:
    • dates["Feb"][0] = 29
    • for x in dates:
    • if x != "Jan":
    • dates[x][1] += 1
    • if month in dates:
    • if day >= 1 and day <= dates[month][0]:
    • total_day = dates[month][1] + day
    • day_type = week_day(day)
    • total_mark = None
    • mark = None
    • if str(day).endswith("1") and str(day) != "11":
    • mark = "st"
    • elif str(day).endswith("2") and str(day) != "12":
    • mark = "nd"
    • elif str(day).endswith("3") and str(day) != "13":
    • mark = "rd"
    • else:
    • mark = "th"
    • if str(total_day).endswith("1") and str(day) != "11":
    • total_mark = "st"
    • elif str(total_day).endswith("2") and str(day) != "12":
    • total_mark = "nd"
    • elif str(total_day).endswith("3") and str(day) != "13":
    • total_mark = "rd"
    • else:
    • total_mark = "th"
    • return f"The {day}{mark} of {full_months[month]} is a {day_type} and the {total_day}{total_mark} day of {year}."
    • return "Invalid Date"
Loading more items...