Start a new Kumite
AllAgda (Beta)BF (Beta)CCFML (Beta)ClojureCOBOL (Beta)CoffeeScriptCommonLisp (Beta)CoqC++CrystalC#D (Beta)DartElixirElm (Beta)Erlang (Beta)Factor (Beta)Forth (Beta)Fortran (Beta)F#GoGroovyHaskellHaxe (Beta)Idris (Beta)JavaJavaScriptJulia (Beta)Kotlinλ Calculus (Beta)LeanLuaNASMNim (Beta)Objective-C (Beta)OCaml (Beta)Pascal (Beta)Perl (Beta)PHPPowerShell (Beta)Prolog (Beta)PureScript (Beta)PythonR (Beta)RacketRaku (Beta)Reason (Beta)RISC-V (Beta)RubyRustScalaShellSolidity (Beta)SQLSwiftTypeScriptVB (Beta)
Show only mine

Kumite (ko͞omiˌtā) is the practice of taking techniques learned from Kata and applying them through the act of freestyle sparring.

You can create a new kumite by providing some initial code and optionally some test cases. From there other warriors can spar with you, by enhancing, refactoring and translating your code. There is no limit to how many warriors you can spar with.

A great use for kumite is to begin an idea for a kata as one. You can collaborate with other code warriors until you have it right, then you can convert it to a kata.

Ad
Ad

Story:
On Mars there exists a company, lets name this company Martion.

This company has an issue with some of their data, you are hired to solve the following issue.

The issue is comming from a larger input than the output, it is more work to change the output field (this impacts thousands of programs), this is why a new function must be build to get the correct data into the output field without changing the output field size.

Scenario:
Input =
EX-RATE-I

Output =
EX-RATE-O
AMT-DECIMALS
ERR-FIELD

Assignment:
Move input EX-RATE to Output EX-RATE and determine how many DECIMALS are moved to Output and note them in AMT-DECIMALS. (Pre decimal numbers are leading)
If Input EX-RATE does not fit into Output EX-RATE, move "Error" to ERR-FIELD - NOTE: it is allowed to round the decimals if possible ( You are expected to do this ).

123456*8901       
       IDENTIFICATION DIVISION.
       PROGRAM-ID. Sqeezing.
      
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 EX-RATE-I            PIC S9(10)V9(08).
       01 EX-RATE-O            PIC  9(09).
       01 AMT-DECIMALS         PIC  9(01).
       01 ERR-FIELD            PIC  X(5).
       01 POINTER1             PIC  9(02).
       01 AMT-PRE-DECI         PIC  9(02).
       01 WHOLE-NB             PIC  9(02).
      
       PROCEDURE DIVISION.
           GOBACK.
      
       R000-SQEEZE SECTION.
           EVALUATE TRUE
           WHEN EX-RATE-I (1:10) = '0000000000'
                MOVE 0                         TO AMT-PRE-DECI
                MOVE 11                        TO POINTER1            
           WHEN EX-RATE-I (1:9) = '000000000'
                MOVE 1                         TO AMT-PRE-DECI
                MOVE 10                        TO POINTER1            
           WHEN EX-RATE-I (1:8) = '00000000'
                MOVE 2                         TO AMT-PRE-DECI
                MOVE 9                         TO POINTER1            
           WHEN EX-RATE-I (1:7) = '0000000'
                MOVE 3                         TO AMT-PRE-DECI
                MOVE 8                         TO POINTER1            
           WHEN EX-RATE-I (1:6) = '000000'
                MOVE 4                         TO AMT-PRE-DECI
                MOVE 7                         TO POINTER1            
           WHEN EX-RATE-I (1:5) = '00000'
                MOVE 5                         TO AMT-PRE-DECI
                MOVE 6                         TO POINTER1            
           WHEN EX-RATE-I (1:4) = '0000'
                MOVE 6                         TO AMT-PRE-DECI
                MOVE 5                         TO POINTER1            
           WHEN EX-RATE-I (1:3) = '000'
                MOVE 7                         TO AMT-PRE-DECI
                MOVE 4                         TO POINTER1
           WHEN EX-RATE-I (1:2) = '00'
                MOVE 8                         TO AMT-PRE-DECI
                MOVE 3                         TO POINTER1            
           WHEN EX-RATE-I (1:1) = '0'
                MOVE 9                         TO AMT-PRE-DECI
                MOVE 2                         TO POINTER1            
           WHEN OTHER
                MOVE 10                        TO AMT-PRE-DECI
                MOVE 1                         TO POINTER1
           END-EVALUATE
            
           EVALUATE TRUE
           WHEN EX-RATE-I(11:8) = '00000000'
                MOVE 0                         TO AMT-DECIMALS
           WHEN EX-RATE-I(12:7) = '0000000'
                MOVE 1                         TO AMT-DECIMALS
           WHEN EX-RATE-I(13:6) = '000000'
                MOVE 2                         TO AMT-DECIMALS
           WHEN EX-RATE-I(14:5) = '00000'
                MOVE 3                         TO AMT-DECIMALS
           WHEN EX-RATE-I(15:4) = '0000'
                MOVE 4                         TO AMT-DECIMALS
           WHEN EX-RATE-I(16:3) = '000'
                MOVE 5                         TO AMT-DECIMALS
           WHEN EX-RATE-I(17:2) = '00'
                MOVE 6                         TO AMT-DECIMALS
           WHEN EX-RATE-I(18:1) = '0'
                MOVE 7                         TO AMT-DECIMALS
           WHEN OTHER
                MOVE 8                         TO AMT-DECIMALS
           END-EVALUATE
           
           MOVE SPACES                         TO ERR-FIELD
      
           EVALUATE TRUE
           WHEN AMT-PRE-DECI = 10 AND AMT-DECIMALS >= 0
                MOVE 'ERROR'                   TO ERR-FIELD
                MOVE 0                         TO AMT-DECIMALS
                MOVE 0                         TO EX-RATE-O
           WHEN AMT-PRE-DECI = 9 AND AMT-DECIMALS > 0
                MOVE EX-RATE-I(POINTER1:9)     TO EX-RATE-O
                IF   EX-RATE-I(11:1) >= 5
                     ADD 1                     TO EX-RATE-O
                END-IF
                MOVE 0                         TO AMT-DECIMALS
           WHEN AMT-PRE-DECI = 8 AND AMT-DECIMALS > 1
                MOVE EX-RATE-I(POINTER1:9)     TO EX-RATE-O
                IF   EX-RATE-I(12:1) >= 5
                     ADD 1                     TO EX-RATE-O
                END-IF
                MOVE 1                         TO AMT-DECIMALS
           WHEN AMT-PRE-DECI = 7 AND AMT-DECIMALS > 2
                MOVE EX-RATE-I(POINTER1:9)     TO EX-RATE-O
                IF   EX-RATE-I(13:1) >= 5
                     ADD 1                     TO EX-RATE-O
                END-IF
                MOVE 2                         TO AMT-DECIMALS
           WHEN AMT-PRE-DECI = 6 AND AMT-DECIMALS > 3
                MOVE EX-RATE-I(POINTER1:9)     TO EX-RATE-O
                IF   EX-RATE-I(14:1) >= 5
                     ADD 1                     TO EX-RATE-O
                END-IF
                MOVE 3                         TO AMT-DECIMALS
           WHEN AMT-PRE-DECI = 5 AND AMT-DECIMALS > 4
                MOVE EX-RATE-I(POINTER1:9)     TO EX-RATE-O
                IF   EX-RATE-I(15:1) >= 5
                     ADD 1                     TO EX-RATE-O
                END-IF
                MOVE 4                         TO AMT-DECIMALS
           WHEN AMT-PRE-DECI = 4 AND AMT-DECIMALS > 5
                MOVE EX-RATE-I(POINTER1:9)     TO EX-RATE-O
                IF   EX-RATE-I(16:1) >= 5
                     ADD 1                     TO EX-RATE-O
                END-IF
                MOVE 5                         TO AMT-DECIMALS
           WHEN AMT-PRE-DECI = 3 AND AMT-DECIMALS > 6
                MOVE EX-RATE-I(POINTER1:9)     TO EX-RATE-O
                IF   EX-RATE-I(17:1) >= 5
                     ADD 1                     TO EX-RATE-O
                END-IF
                MOVE 6                         TO AMT-DECIMALS
           WHEN AMT-PRE-DECI = 2 AND AMT-DECIMALS > 7
                MOVE EX-RATE-I(POINTER1:9)     TO EX-RATE-O
                IF   EX-RATE-I(18:1) >= 5
                     ADD 1                     TO EX-RATE-O
                END-IF
                MOVE 7                         TO AMT-DECIMALS
           WHEN OTHER
                COMPUTE WHOLE-NB = AMT-PRE-DECI 
                                 + AMT-DECIMALS
                END-COMPUTE
                MOVE EX-RATE-I(POINTER1:WHOLE-NB) 
                                               TO EX-RATE-O
           END-EVALUATE

This kumite shows how to use stringizers to avoid confusing assertion messages in C++ tests.

For detailed description, see this Help article: Adding Custom Stringizers.

//See preloaded part for definition of stringizers

int test() { return 0; }

Hello Everybody I would like your help to improve the code quality of this very old, simple and scrappy code I made when I started to code as a hobby.

import random


def code():
    print('Guess a number between 1 and 25')

    # secret number
    secret_number = random.randint(1, 25)

    guess_count = 0
    guess_limit = 5

    # mainloop
    while guess_count < guess_limit:
        try:
            guess = int(input('\nGuess: '))
            guess_count += 1

        except ValueError:
            print("\nInvalid Input!")
            continue

        if guess > secret_number:
            print('\nNo, too big.')
            guess_count += 1

        elif guess < secret_number:
            print('\nNo, too small.')
            guess_count += 1

        elif guess == secret_number:
            print('\nWow, you are a actully are a true mind reader!')
            print(f"The number was {secret_number} indeed! ")
            reply = input('\nDo you want to play guess again? (yes/no) - ')
            if reply == 'yes':
                code()
            else:
                exit()

        if guess_count == 5:
            print('\nSorry, you have failed:(')
            print(f'\nThe secret number was {secret_number}.')

            replay = input('\nDo you want to play Guess game again? (yes/no) - ')
            if replay == 'yes':
                code()
            elif replay == 'no':
                exit()
            else:
                print("I don't understand")


code()

A customer pays a goods for 100 (price) and they must pay additonal tax 10 (VAT rate 10%). The final price will be 110.

If the customer pays goods with price greater than or equal 1000, the customers will pay additional luxury tax with rate 30%.

We need your help to make a formula to calculate the final price (include tax).

Information:
VAT rate = 10%
Luxury tax rate* = 30%
*imposed only if the goods and services price greater than or equal 1000

function finalPrice(price) {
  var vat = 0.1
  var lux = 0.3
  
  if(price >= 1000) {
  return Math.floor(price * (1+vat+lux))
    } else {
      return Math.floor(price * (1+vat))
    }
}

Restaurant have to calcute the final price which include Value Added Tax(VAT) and service charge.

Service charge only calculated for dine in.

We need your help to make a function to calculate the final price.

Note:
-VAT rate is 10%
-service charge rate is 5%
-service charge is taxable
-the final price should be round down.

Example:
-if the price is 100 and take away, the final price would be 110
-if the price is 100 and dine in, the final price would be 105

function finalPrice(price,dineIn) {
  var service = 0.05
  var VAT = 0.1
  
  if(dineIn == true) {
    return Math.floor(price * (1 +service) * (1+ VAT)) }
  else {
    return Math.floor(price * (1+ VAT))
  }
   
  
}

תרגול טיפוסי נתונים מורכבים
במסעדה של טוויטי רוצים לעקוב אחרי העובדים במסעדה ואחרי הגילאים שלהם על מנת לנהל את השכר שלהם בצורה מדויקת וטובה. כאן בדיוק אתם נכנסים לתמונה: עליכם לכתוב אפליקציה בת שתי מחלקות שתנהל את רשימות העובדים במסעדה.
מחלקת ה-Main
מחלקה זו תכיל את את השגרה הראשית main והיא תציג תפריט למשתמש עד אשר הוא יבחר לצאת. כל פעם שהמשתמש בוחר פעולה מהתפריט יש להפעיל את המתודות המתאימות.
התפריט יכיל את האופציות הבאות:

  1. הוספת עובד
  2. הסרת עובד
  3. בדיקה האם עובד קיים ברשימות העובדים
  4. הדפסת כל העובדים במסעדה
  5. שינוי גיל של עובד
    יש להגדיר קבועים לייצוג האפשרויות האלו

מחלקת המסעדה – Restaurant
למחלקה זו data member אחד:
private HashMap<String, Integer> emploees;
ה Data Member שלנו הוא מטיפוס HashMap.
מתודות:
הוספת עובד
public boolean addEmployee(String name, int age)
ערך החזר:
האם הצלחנו להוסיף את העובד.
הפרמטרים המתקבלים:
שם העובד
גל העובד
שני נתונים אלו ישמרו בתוך הרשימה שלנו.
כיצד ניתן לשמור את שניהם יחדיו? כנראה שאחד מהערכים יהיה המפתח (הממוין), והשני יהיה הערך הנשמר. חישבו היטב מה יהיה ממוין ומה יהיה הערך.
כאשר אנו קולטים את שני הנתונים הללו יש לבדוק את הדברים הבאים:
השדות הללו מכילים ערכים – הרי לא הגיוני שנקלוט עובד שאין לו שם.
לא קיים עובד עם שם כזה במערכת – כי אם הוא כבר קיים המערכת תספוג תעופה

פיטור עובד
public boolean removeEmployee(String name)
הסרה מהמסעדה של העובד
יש לבדוק שהעובד באמת קיים במערכת

בדיקה האם קיים עובד במערכת
private boolean isEmployeeExist(String name)
מקבלת שם עובד ובודקת האם הוא קיים ברשימה
זוהי מתודה פרטית למחלקת המסעדה, חישבו היכן היא תהיה שימושית.

הדפסת רשימת העובדים
public void printEmployees()

יש לטפל במקרה ולא קיימים עובדים במסעדה באמצעות הדפסה מיוחדת.

import java.util.InputMismatchException;
import java.util.Scanner;

public class Main {
    private static Scanner input = new Scanner(System.in);
    private static final int EXIT = -999;
    public static Restaurant restaurant;
    final static int ADD_EMPLOYEE = 1;
    final static int REMOVE_EMPLOYEE = 2;
    final static int IS_EMPLOYEE_EXIST = 3;
    final static int PRINT_EMPLOYEES = 4;
    final static int CHANGE_EMPLOYEE_AGE = 5;

    public static void main(String[] args) {
        System.out.println("Choose your option from the list below or if you want to stop enter -999");
        System.out.println("Press "+ADD_EMPLOYEE+" to add employee");
        System.out.println("Press "+REMOVE_EMPLOYEE+" to remove employee");
        System.out.println("Press "+IS_EMPLOYEE_EXIST+" to check if employee exist");
        System.out.println("Press "+PRINT_EMPLOYEES+" to print employees");
        System.out.println("Press "+CHANGE_EMPLOYEE_AGE+" to change age");

        int option = input.nextInt();
        while (option > 5 || (option < 1  && option!= EXIT )) {
            System.out.println("Wrong option number choose your option again");
            option = input.nextInt();
        }

        restaurant = new Restaurant();

        while (option != EXIT) {
            turn(option);

            System.out.println("Choose your option if you want to stop enter -999");
            option = input.nextInt();
            while (option > 5 || (option < 1  && option!= EXIT )) {
                System.out.println("Wrong option number choose your option again");
                option = input.nextInt();
            }
        }

    }

    private static void turn(int option) {
        switch (option) {
            case ADD_EMPLOYEE:
                addEmployee();

                break;
            case REMOVE_EMPLOYEE:
                removeEmployee();

                break;
            case IS_EMPLOYEE_EXIST:
                isEmployeeExist();

                break;
            case PRINT_EMPLOYEES:
                restaurant.printEmployees();

                break;
            case CHANGE_EMPLOYEE_AGE:
                changeEmployeeAge();

                break;
            default:
                break;
        }
    }

    private static void addEmployee() {
        String name;
        int age;

        System.out.println("Enter employee's name");
        name = input.next();
        System.out.println("Enter employee's age");
        age = input.nextInt();
        if (restaurant.addEmployee(name, age)) {
            System.out.println("You added the employee name to " + name + " and age to " + age);
        } else {
            if (name.isEmpty()) {
                System.out.println("You did not entered a name");
            } else {
                System.out.println("The employee " + name + " already exist");
            }
        }
    }

    private static void removeEmployee() {
        String name;

        System.out.println("Enter employee's name");
        name = input.next();
        if (restaurant.removeEmployee(name)) {
            System.out.println("You remvoed the employee " + name);
        } else {
            System.out.println("The employee " + name + " does not exist");
        }
    }

    private static void isEmployeeExist() {
        String name;

        System.out.println("Enter employee's name");
        name = input.next();
        if (restaurant.isEmployeeExist(name)) {
            System.out.println("Employee " + name + " exist");
        } else {
            System.out.println("Employee " + name + "  does not exist");
        }

    }

    private static void changeEmployeeAge() {
        String name;
        int age;

        System.out.println("Enter employee's name");
        name = input.next();
        System.out.println("Enter employee's age");
        age = input.nextInt();
        if (restaurant.setAge(name, age)) {
            System.out.println("You updated the employee " + name + " age to " + age);
        } else {
            System.out.println("The employee " + name + " does not exist");
        }
    }
}

import java.util.HashMap;

public class Restaurant {
    private HashMap<String, Integer> emploees;
    public Restaurant(){
        emploees = new HashMap<String, Integer>();
    }

    public boolean addEmployee(String name, int age) {
        if (this.isEmployeeExist(name) || name.isEmpty()) {
            return false;
        } else {
            this.emploees.put(name, age);
            return true;
        }
    }

    public boolean removeEmployee(String name) {
        if(this.isEmployeeExist(name)){
            this.emploees.remove(name);
            return true;
        }
        return false;
    }

    public boolean isEmployeeExist(String name){
        return (this.emploees.containsKey(name));
    }

    public void printEmployees() {
        if(this.emploees.isEmpty()){
            System.out.println("This restaurant has no workers");
        }
        else{
            for(String key : this.emploees.keySet()){
                System.out.println(key+" "+ this.emploees.get(key));

            }
        }

    }

    public boolean setAge(String name,int age){
        if(this.emploees.containsKey(name)){
            this.emploees.replace(name,age);
            return true;
        }
        return false;


    }



}
Algorithms
Logic
Lists
Data Structures

Given an input list. Example [1,2,3,4,5,6] split it into a new list, where the items contained are split into longer and longer length lists in the parent list. Like: [[1], [2, 3], [4, 5, 6]].

If the list does not fill the chunk completly, fill it up party. [1,2,3,4] => [[1], [2, 3], [4]]

More examples:

[1, 2, 3, 4, 5, 6] => [[1], [2, 3], [4, 5, 6]]

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] => [[1], [2, 3], [4, 5, 6], [7, 8, 9, 10], [11, 12, 13, 14, 15], [16]]

def variable_chunks(lst):
    if lst == []:
        return [[]]
    result = []
    y = 0
    for x in range(1, len(lst) + 1):
        result.append(lst[y:y+x])
        y = y+x
    return [x for x in result if x != []]
// The operation with the number "1" goes first
var add1 = 0
var add2 = 0

var minus1 = 0
var minus2 = 0

var divide1 = 0
var divide2 = 0

var times1 = 0
var times2 = 0
//change the numbers in the varibles above

console.log("Addition" , add1 + add2) //Addition

console.log("Subtraction", minus1-minus2) //Subtraction

console.log("Division" , divide1/divide2) //Division

console.log("Multiplication" , times1*times2) //Multiplication

Make a file get downloaded.

function download(filename, text) {
          var element = document.createElement("a");
          element.setAttribute(
            "href",
            "data:text/plain;charset=utf-8," + encodeURIComponent(text)
          );
          element.setAttribute("download", filename);

          element.style.display = "none";
          document.body.appendChild(element);

          element.click();

          document.body.removeChild(element);
        }

        window.onload = function() {
            var text = "downloaded file";
            var filenames = "file";
            var filename = filenames + ".txt";

            download(filename, text);
        }

You are building a calendar app. You have the following task assigned to you:

Build a function that receives a single parameter - a list of time intervals when the user is not available - and returns a list of the intervals when the user will be free in the same day. Consider that all times are restricted to 30 minutes increments, eg “12:00”, “00:30”, “17:30”. No input validation is necessary.

Both the parameter and the return value should be a list of tuples, for example, the following list:

[(“12:00”, “13:00”), (“14:00”, “17:00”)]

Means that the user is busy from 12:00 to 13:00 (let’s say it’s his lunch time) and from 14:00 to 17:00 (let’s say he’ll be in a long meeting). Calling the function with the arguments, like:

Should return the time intervals when the user is available, so, in this case, it would be:

[(“00:00”,“12:00”), (“13:00”, “14:00”), (“17:00”, “24:00”)]

def get_available_times(hours):
    available_times = {}
    i = 0
    for i in range(0, len(hours)):
        if hours[i][0] == '00:00':
            continue
        if i == 0:
            available_times['00:00'] = hours[i][0]
        else:
            available_times[hours[i-1][1]] = hours[i][0]
        if i == (len(hours) - 1) and hours[i][1] != '24:00':
            available_times[hours[i][1]] = '24:00'
    return list(available_times.items())