diff --git a/golang/main.go b/golang/main.go index eb14a8a..d05b5cf 100644 --- a/golang/main.go +++ b/golang/main.go @@ -3,6 +3,7 @@ package main import ( "fmt" "math" + "strings" ) func main() { @@ -21,6 +22,59 @@ func main() { for _, element := range xValues { Problem(a, b, element) } + + fmt.Printf("-------------------------------\n") + fmt.Printf("Codewars section\n") + fmt.Printf("-------------------------------\n") + + fmt.Printf("Is hero survive?\n") // 1 + fmt.Printf("bullets = 10, dragons = 5, result = %t\n", Hero(10, 5)) + fmt.Printf("\n") + + fmt.Printf("School Paperwork\n") // 2 + fmt.Printf("n = 5, m = 5, result = %d\n", paperwork(5, 5)) + fmt.Printf("n = -5, m = 5, result = %d\n", paperwork(-5, 5)) + fmt.Printf("\n") + + fmt.Printf("Count the Monkeys\n") // 3 + fmt.Printf("n = 10, result = %v\n", monkeyCount(10)) + fmt.Printf("\n") + + fmt.Printf("Counting sheep\n") // 4 + sheeps := []bool{ + true, true, true, false, + true, true, true, true, + true, false, true, false, + true, false, false, true, + true, true, true, true, + false, false, true, true, + } + fmt.Printf("sheeps = %v, result = %v\n", sheeps, CountSheeps(sheeps)) + fmt.Printf("\n") + + fmt.Printf("Even or Odd\n") // 5 + fmt.Printf("number = 2, result = %s\n", EvenOrOdd(2)) + fmt.Printf("number = 3, result = %s\n", EvenOrOdd(3)) + fmt.Printf("\n") + + fmt.Printf("Sum of Minimums\n") // 6 + arrayForSumMinimums := [][]int{ + {1, 2, 3, 4, 5}, + {5, 6, 7, 8, 9}, + {20, 21, 34, 56, 100}, + } + fmt.Printf("numbers = %v, result = %d\n", arrayForSumMinimums, sumOfMinimums(arrayForSumMinimums)) + fmt.Printf("\n") + + fmt.Printf("Find all occurrences of an element in an array\n") // 7 + arrayForOccurrences := []int{ + 6, 9, 3, 4, 3, 82, 11, + } + fmt.Printf("numbers = %v, n = 3, result = %d\n", arrayForOccurrences, findAll(arrayForOccurrences, 3)) + fmt.Printf("\n") + + fmt.Printf("Polish alphabet\n") // 7 + fmt.Printf("input = Jędrzej Błądziński, result = %s\n", correctPolishLetters("Jędrzej Błądziński")) } func Problem(a, b, x float64) { @@ -31,3 +85,109 @@ func Problem(a, b, x float64) { func Log(base, x float64) float64 { return math.Log(x) / math.Log(base) } + +// CountSheeps +func CountSheeps(numbers []bool) int { + if numbers != nil { + var answer int = 0 + for _, item := range numbers { + if item { + answer++ + } + } + return answer + } + return 0 +} + +// EvenOrOdd +func EvenOrOdd(number int) string { + if number%2 == 0 { + return "Even" + } else { + return "Odd" + } +} + +// monkeyCount +func monkeyCount(n int) []int { + count := make([]int, n) + for i := 1; i <= n; i++ { + count[i-1] = i + } + return count +} + +// Hero and dragons +func Hero(bullets, dragons int) bool { + if bullets < dragons*2 { + return false + } else { + return true + } +} + +// correctPolishLetters +func correctPolishLetters(str string) string { + str = strings.Replace(str, "ą", "e", -1) + str = strings.Replace(str, "ć", "e", -1) + str = strings.Replace(str, "ę", "e", -1) + str = strings.Replace(str, "ł", "e", -1) + str = strings.Replace(str, "ń", "e", -1) + str = strings.Replace(str, "ó", "e", -1) + str = strings.Replace(str, "ś", "e", -1) + str = strings.Replace(str, "ź", "e", -1) + str = strings.Replace(str, "ż", "e", -1) + return str +} + +// numberOccurrences +func numberOccurrences(array []int, n int) int { + var result int = 0 + for _, item := range array { + if item == n { + result++ + } + } + return result +} + +func findAll(array []int, n int) []int { + result := make([]int, numberOccurrences(array, n)) + var currentIndex int = 0 + + for index, item := range array { + if item == n { + result[currentIndex] = index + currentIndex++ + } + } + + return result +} + +// sumOfMinimums +func sumOfMinimums(numbers [][]int) int { + var result int = 0 + var minimum int + for _, i := range numbers { + minimum = i[0] + for _, j := range i { + if minimum > j { + minimum = j + } + } + result += minimum + } + + return result +} + +// School Paperwork +func paperwork(n int, m int) int { + if n < 0 || m < 0 { + return 0 + } else { + return n * m + } +} diff --git a/python/src/main.py b/python/src/main.py index 54215bf..f71d4c7 100644 --- a/python/src/main.py +++ b/python/src/main.py @@ -11,6 +11,84 @@ def Log(base: float, x: float): return math.log(x) / math.log(base) +# Sum of Minimums +def sum_of_minimums(numbers): + result = 0 + for i in range(len(numbers)): + minimum = None + for j in range(len(numbers[0])): + if (minimum is None or minimum > numbers[i][j]): + minimum = numbers[i][j] + result += minimum + return result + + +# Find all occurrences of an element in an array +def find_all(array, n): + result = [] + for idx, element in enumerate(array): + if element == n: + result.append(idx) + return result + + +# Polish alphabet +def correct_polish_letters(st): + st = st.replace('ą', 'a') + st = st.replace('ć', 'c') + st = st.replace('ę', 'e') + st = st.replace('ł', 'l') + st = st.replace('ń', 'n') + st = st.replace('ó', 'o') + st = st.replace('ś', 's') + st = st.replace('ź', 'z') + st = st.replace('ż', 'z') + return st + + +# Beginner Series #1 School Paperwork +def paperwork(n, m): + if n < 0 or m < 0: + return 0 + else: + return n * m + + +# Count the Monkeys! +def monkey_count(n): + count = [] + for i in range(n): + count.append(i + 1) + return count + + +# Is he gonna survive? +def hero(bullets, dragons): + if bullets < dragons * 2: + return False + else: + return True + + +# Even or Odd +def even_or_odd(number): + if number % 2 == 0: + return "Even" + else: + return "Odd" + + +# Counting sheep +def count_sheeps(sheep): + if sheep is not None: + answer = 0 + for item in sheep: + if item: + answer += 1 + return answer + return 0 + + if __name__ == "__main__": a = 1.1 b = 0.09