Fundamentals
Fundamentals
Games
def multiply_and_add_one(a, b): if a < 0 and b < 0: return multiply_and_add_one(abs(a), abs(b)) elif a > b: return multiply_and_add_one(b, a) elif b != 0: return a + multiply_and_add_one(a, b - 1) elif b == 0: return 1 else: return 0
- def multiply_and_add_one(a, b):
if a > b:- if a < 0 and b < 0:
- return multiply_and_add_one(abs(a), abs(b))
- elif a > b:
- return multiply_and_add_one(b, a)
- elif b != 0:
- return a + multiply_and_add_one(a, b - 1)
- elif b == 0:
- return 1
- else:
- return 0
import codewars_test as test from solution import multiply_and_add_one @test.describe("Multiply and Add One Tests") def _(): @test.it("Basic tests") def test_case(): test.assert_equals(multiply_and_add_one(2, 3), 7) test.assert_equals(multiply_and_add_one(4, 5), 21) test.assert_equals(multiply_and_add_one(0, 10), 1) test.assert_equals(multiply_and_add_one(-1, 5), -4) test.assert_equals(multiply_and_add_one(1, -5), -4) test.assert_equals(multiply_and_add_one(-1, -5), 6)
- import codewars_test as test
- from solution import multiply_and_add_one
- @test.describe("Multiply and Add One Tests")
- def _():
- @test.it("Basic tests")
- def test_case():
- test.assert_equals(multiply_and_add_one(2, 3), 7)
- test.assert_equals(multiply_and_add_one(4, 5), 21)
- test.assert_equals(multiply_and_add_one(0, 10), 1)
- test.assert_equals(multiply_and_add_one(-1, 5), -4)
- test.assert_equals(multiply_and_add_one(1, -5), -4)
- test.assert_equals(multiply_and_add_one(-1, -5), 6)
20 characters saved
min() key is now a tuple generator
tuples are compared (sorted) by index 0 then on conflict subsequent indices
index 0 is abs() like before
index 1 is negative reciprocal so positive numbers are sorted before negative numbers
# Or just make string equality case insensitive. class CaseInsensitiveString(str): def __eq__(self, other): return other.lower() == self.lower() fun=lambda x: CaseInsensitiveString(x)
fun=str.upper- # Or just make string equality case insensitive.
- class CaseInsensitiveString(str):
- def __eq__(self, other):
- return other.lower() == self.lower()
- fun=lambda x: CaseInsensitiveString(x)
def max_number(n): d = 0 # Get number of digits while n // (10 ** d): d += 1 # Get digit of n with length d at index i def digit(n, d, i): return int((n // (10 ** (d - i - 1)))) % 10 # Swap digits of n in place def swap(n, d, i, j): di = digit(n, d, i) dj = digit(n, d, j) n -= di * (10 ** (d - i - 1)) n -= dj * (10 ** (d - j - 1)) n += di * (10 ** (d - j - 1)) n += dj * (10 ** (d - i - 1)) return n x = None # Bubble sort an integer in place while x != n: x = n for i in range(d): di = digit(n, d, i) for j in range(d - 1, i, -1): dj = digit(n, d, j) # Swap condition if you'd prefer smallest int if di < dj: n = swap(n, d, i, j) break if x != n: break return n
import java.util.Arrays;- def max_number(n):
- d = 0
- # Get number of digits
- while n // (10 ** d):
- d += 1
- # Get digit of n with length d at index i
- def digit(n, d, i):
- return int((n // (10 ** (d - i - 1)))) % 10
- # Swap digits of n in place
- def swap(n, d, i, j):
- di = digit(n, d, i)
- dj = digit(n, d, j)
- n -= di * (10 ** (d - i - 1))
- n -= dj * (10 ** (d - j - 1))
- n += di * (10 ** (d - j - 1))
- n += dj * (10 ** (d - i - 1))
- return n
public class MaxNumber {public static long print(long number) {return number}}- x = None
- # Bubble sort an integer in place
- while x != n:
- x = n
- for i in range(d):
- di = digit(n, d, i)
- for j in range(d - 1, i, -1):
- dj = digit(n, d, j)
- # Swap condition if you'd prefer smallest int
- if di < dj:
- n = swap(n, d, i, j)
- break
- if x != n:
- break
- return n
import codewars_test as test from solution import max_number @test.describe("max_number") def _(): @test.it("Basic tests") def test_case(): test.assert_equals(max_number(4), 4) test.assert_equals(max_number(12), 21) test.assert_equals(max_number(101), 110) test.assert_equals(max_number(1234), 4321) test.assert_equals(max_number(400000005000007000), 754000000000000000) test.assert_equals(max_number(307778062924466824), 988777666444322200)
import static org.junit.Assert.assertEquals;import org.junit.Test;import java.util.Random;- import codewars_test as test
- from solution import max_number
public class MaxNumberTest {@Testpublic void testFour() {assertEquals(4, MaxNumber.print(4));}@Testpublic void testTwelve() {assertEquals(21, MaxNumber.print(12));}@Testpublic void testOneHundred() {assertEquals(110, MaxNumber.print(101));}@Testpublic void testHuge1() {assertEquals(754000000000000000L, MaxNumber.print(400000005000007000L));}@Testpublic void testHuge2() {assertEquals(988777666444322200L, MaxNumber.print(307778062924466824L));}}- @test.describe("max_number")
- def _():
- @test.it("Basic tests")
- def test_case():
- test.assert_equals(max_number(4), 4)
- test.assert_equals(max_number(12), 21)
- test.assert_equals(max_number(101), 110)
- test.assert_equals(max_number(1234), 4321)
- test.assert_equals(max_number(400000005000007000), 754000000000000000)
- test.assert_equals(max_number(307778062924466824), 988777666444322200)
def multiply_and_add_one(a, b): if a > b: return multiply_and_add_one(b, a) elif b != 0: return a + multiply_and_add_one(a, b - 1) elif b == 0: return 1 else: return 0
- def multiply_and_add_one(a, b):
return (a * b) + 1- if a > b:
- return multiply_and_add_one(b, a)
- elif b != 0:
- return a + multiply_and_add_one(a, b - 1)
- elif b == 0:
- return 1
- else:
- return 0
package kata func SumLetters(a string, b string) bool { return len(a) == len(b) && len(a) > 0 }
def verify_sum(a, b)a&&b&&a.sum==b.sum||falseend- package kata
- func SumLetters(a string, b string) bool {
- return len(a) == len(b) && len(a) > 0
- }
package kata_test import ( . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" . "codewarrior/kata" ) func dotest(a string, b string, expected bool) { actual := SumLetters(a, b) Expect(actual).To(Equal(expected)) } var _ = Describe("Tests", func() { It("Example 1", func() { dotest("Sebastian", "Patricia", false) }) It("Example 2", func() { dotest("Anna", "Nana", true) }) It("Example 3", func() { dotest("John", "", false) }) It("Example 4", func() { dotest("", "", false) }) })
# From Ruby 3.0, RSpec is used under the hood.# See https://rspec.info/# Defaults to the global `describe` for backwards compatibility, but `RSpec.desribe` works as well.describe "Example" doit "should return the sum" doexpect(verify_sum("Sebastian", "Patricia")).to eq(false)expect(verify_sum("Anna", "Nana")).to eq(true)expect(verify_sum("John", nil)).to eq(false)expect(verify_sum(nil, nil)).to eq(false)# The following is still supported, but new tests should now use them.# Test.assert_equals(add(1, 1), 2)endend- package kata_test
- import (
- . "github.com/onsi/ginkgo"
- . "github.com/onsi/gomega"
- . "codewarrior/kata"
- )
- func dotest(a string, b string, expected bool) {
- actual := SumLetters(a, b)
- Expect(actual).To(Equal(expected))
- }
- var _ = Describe("Tests", func() {
- It("Example 1", func() {
- dotest("Sebastian", "Patricia", false)
- })
- It("Example 2", func() {
- dotest("Anna", "Nana", true)
- })
- It("Example 3", func() {
- dotest("John", "", false)
- })
- It("Example 4", func() {
- dotest("", "", false)
- })
- })
Fundamentals
Games
Fundamentals
Games
describe "basic tests" do specify { expect(riddle("Two?")).to eq(3) } specify { expect(riddle("Ten?")).to eq(3) } specify { expect(riddle("Three?")).to eq(5) } end
test.assert_equals(riddle("Two?"), 3)test.assert_equals(riddle("Ten?"), 3)test.assert_equals(riddle("Three?"), 5)- describe "basic tests" do
- specify { expect(riddle("Two?")).to eq(3) }
- specify { expect(riddle("Ten?")).to eq(3) }
- specify { expect(riddle("Three?")).to eq(5) }
- end