Skip to content

Marconymous/muetzilla-advent-calendar

Repository files navigation

Solutions for Muetzilla's Advent Calendar

Link To the Advents Calendar

Content

Problem 1

Hello World in a new Language: Go

Source Code

package main

func main() {
	println("Hello World!")
}

Problem 2

Calculate the LCM from numbers 1 - 10

Source Code

  • The Function size_t lcm(size_t a, size_t b) takes 2 numbers as parameters.
  • The Bigger number of the two is used as a start.
  • Then the program counts up until the number is divisible by both numbers.
size_t lcm(size_t a, size_t b) {
    int max = (a > b) ? a : b;

    while (1) {
        if (max % a == 0 && max % b == 0) {
            return max;
        }

        ++max;
    }
}

Problem 3

Calculate the GCD of all numbers from 1 - 10

Source Code

  • The Function private int findGCD(int a, int b) takes 2 numbers as parameters.
  • This Function will be called recursively with the args (b, a % b) until b == 0 and a is returned.
private int findGCD(int a, int b) {
    if (b == 0) return a;
    return findGCD(b, a % b);
}

Problem 4

Create a Calculator using Python which can do (+-*/) operations.

Source Code

  • First the calculation is split into it's parts (numbers & operators)
    • 15 + 53 * 10 => ['15', '+', '53', '*', '10']
  • Then the operations * and / are performed
    • def point_calc(calc):
    • => ['15', '+', '530']
  • Finally the operations + and - are performed
    • def dash_calc(calc):
    • => [545]
split = split_calculation(calculation)
try:
    after_point = point_calc(split)
    after_dash = dash_calc(after_point)
    print(f"The result is: {after_dash}")
except:
    print("The calculation you entered is not Valid!")

Problem 5

Make a Program to calculate the circumference and area of a Shape

Source Code

Enum with functions for shapes enum class Shape(val circumerence: () -> Double, val area: () -> Double)

Problem 6

Create a Program in any JVM Language, which takes user input and converts it to the nth Primenumber

Language used: Scala

Source Code

private def isPrime(n: Int): Boolean = {
    for (i <- 2 to (n / 2)) {
        if (n % i == 0) return false
    }

    true
}

Problem 7

Create a Website to manage wishes

Source Code

Problem 8

Create an algorithm in Rust which can sort an array on numbers

Source Code

fn sort(arr: &mut [i32]) -> &mut [i32] {
    let mut changed = false;
    for i in 0..arr.len() {
        for j in 0..arr.len() - 1 - i {
            if arr[j] > arr[j + 1] {
                let swap = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = swap;
                changed = true
            }
        }

        if !changed {
            return arr;
        }
    }

    return arr;
}

Problem 9

Create a Program which can print songtext from a file line by line. The User can specify the amount it takes for the next line to be printed.

Source Code

public void printsong(long time) throws InterruptedException {
    if (!fileScanner.hasNext())
        return;

    Thread.sleep(time);
    System.out.println(fileScanner.nextLine());

    printsong(time);
}

Problem 10

Create a Rock Paper Scissors Program in JavaScript

To find who won I used array indices.

function getWinner(i, j) {
    function increment(i) {
        return (i + 1) % moves.length;
    }

    if (j === increment(i)) return 'Computer'
    if (j === i) return 'No one'

    return 'Player'
}

Problem 11

Create a Program which takes words as user input and creates new words with them

static Tuple<string, string> SplitString(string s)
{
    var strLen = s.Length / 2;
    return new Tuple<string, string>(s.Substring(0, strLen), s.Substring(strLen, strLen));
}

Problem 12

Create a Program which prints all Palindrome numbers in the Integer range using C++

int reverse_int(int n)
{
    int reversedNumber = 0, remainder;

    while (n != 0)
    {
        remainder = n % 10;
        reversedNumber = reversedNumber * 10 + remainder;
        n /= 10;
    }

    return reversedNumber;
}

Problem 13

Create a Game where you can guess the number generated by the computer

static void run() {
        final int generatedNumber = (int) ((Math.random() * (MAX - MIN)) + MIN)
        while (true) {
            print "Guess a number ($MIN, $MAX) > "
            def i = System.in.newReader().readLine() as Integer

            if (i == generatedNumber) {
                println "You guessed correctly : $i"
                break
            }

            if (i > generatedNumber) {
                println "Your guess was too high"
                if (i < MAX) {
                    MAX = i
                }
            } else {
                println "Your guess was too low"
                if (i > MIN) {
                    MIN = i
                }
        }

    }
}

How To Run The Code

  • Go
    • Install Golang on your local machine
    • Run the following command in a terminal: go run .\ProblemXX.go
  • C / C++
    • Install a C compiler (Ex. GCC)
    • Run the following command: gcc ProblemXX.c -o ProblemXX
    • Run the created .exe / .o file
  • Java
    • Install JDK & JRE
    • Run the following commands in a terminal: javac .\ProblemXX.java; & java ProblemXX
  • Python
    • Install Python on your system
    • Run the following command: python ProblemXX.py
  • Kotlin
    • Install Kotlin
    • Run the following command: kotlinc ProblemXX.kt -include-runtime -d ProblemXX.jar
    • To Run the Problem, run: java -jar ProblemXX.kt
  • Scala
    • Install Scala
    • Run the following commands:
      • Compile: scalac ProblemXX.scala
      • Run: scala ProblemXX
  • Rust
    • Install Rust
    • Create a Cargo Project with: cargo init
    • Paste the Code into the created .rs file
    • Run the program with the following command: cargo run
  • JavaScript
    • Install node
    • Run the following command: node ProblemXX.js