Given the function:
H(x, k) = \dfrac {{x} \times {2 ^ k} - 1} {3}
We're only interested in the pairs of (x, k) that return an odd number. Implement a function $invH$
that receives an integer n and returns two values - x and k in this order - in which $H(invH(n)) = n$
def invH(n):
px, pk = (n >> 1), 1
while px % 4 == 0b10:
px >>= 2
pk = (pk << 2) + 1
k = (pk.bit_length()-1) // 2
if px % 4 == 0:
x = px // 4
return (6*x+1, 2*k+2)
else:
x = (px-1) // 2
return (6*x+5, 2*k+1)
import codewars_test as test
from preloaded import H
from solution import invH
import random
@test.describe("Example")
def test_group():
@test.it("Random Cases, small numbers")
def test_case():
for _ in range(1000):
num = random.randrange(1001, 10000, 2)
x, k = invH(num)
test.assert_equals(H(x, k), num, f'H({x}, {k}) = {H(x,k)}')
@test.it("Random Tests, large numbers")
def test_case():
for _ in range(1000):
num = random.randrange((1 << 1024) + 1, 1 << 1025, 2)
x, k = invH(num)
test.assert_equals(H(x, k), num, f'H({x}, {k}) = {H(x,k)}')
@test.it("Random Tests, really large numbers")
def test_case():
for _ in range(1000):
num = random.randrange((1 << 8192) + 1, 1 << 8193, 2)
x, k = invH(num)
test.assert_equals(H(x, k), num, f'H({x}, {k}) = {H(x,k)}')
class Account { private int balance = 0; public void deposit(int amount) throws IllegalArgumentException { if(amount < 0) { throw new IllegalArgumentException("You cannot deposit negative money!"); } balance += amount; } public void withdraw(int amount) throws IllegalArgumentException { if (amount < 0) { throw new IllegalArgumentException("You cannot withdraw negative money!"); } if(amount > balance) { throw new IllegalArgumentException("You cannot withdraw more than you have!"); } balance -= amount ; } public String getStatement() { return String.valueOf(balance); } }
- class Account {
- private int balance = 0;
void deposit(int amount){- public void deposit(int amount) throws IllegalArgumentException {
- if(amount < 0) {
- throw new IllegalArgumentException("You cannot deposit negative money!");
- }
- balance += amount;
- }
void withdraw(int amount) throws IllegalArgumentException{- public void withdraw(int amount) throws IllegalArgumentException {
- if (amount < 0) {
- throw new IllegalArgumentException("You cannot withdraw negative money!");
- }
- if(amount > balance) {
- throw new IllegalArgumentException("You cannot withdraw more than you have!");
- }
- balance -= amount ;
- }
String getStatement() {- public String getStatement() {
- return String.valueOf(balance);
- }
- }
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.BeforeEach; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; class AccountTest { private Account account; @BeforeEach public void setUp() { this.account = new Account(); } @Test void deposit_adds_money_to_initial_balance() { account.deposit(100); String statement = account.getStatement(); assertEquals("100", statement); } @Test void deposit_adds_different_amount_of_money_to_initial_balance(){ account.deposit(200); String statement = account.getStatement(); assertEquals("200", statement); } @Test void withdraw_of_zero_balance_remains_same(){ account.withdraw(0); String statement = account.getStatement(); assertEquals("0", statement); } @Test void withdraw_reduces_balance_by_amount_withdrawn(){ account.deposit(500); account.withdraw(100); String statement = account.getStatement(); assertEquals("400", statement); } @Test void cannot_withdraw_more_than_you_have() { account.deposit(100); assertThrows(IllegalArgumentException.class, () -> { account.withdraw(200); }); } @Test void cannot_withdraw_negative_money() { assertThrows(IllegalArgumentException.class, () -> { account.withdraw(-100); }); } @Test void cannot_deposit_negative_money() { assertThrows(IllegalArgumentException.class, () -> { account.deposit(-100); }); } }
- import org.junit.jupiter.api.Test;
- import org.junit.jupiter.api.BeforeEach;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- import static org.junit.jupiter.api.Assertions.assertThrows;
- class AccountTest {
- private Account account;
- @BeforeEach
- public void setUp() {
- this.account = new Account();
- }
- @Test
- void deposit_adds_money_to_initial_balance() {
Account account = new Account();- account.deposit(100);
- String statement = account.getStatement();
- assertEquals("100", statement);
- }
- @Test
- void deposit_adds_different_amount_of_money_to_initial_balance(){
Account account = new Account();- account.deposit(200);
- String statement = account.getStatement();
- assertEquals("200", statement);
- }
- @Test
- void withdraw_of_zero_balance_remains_same(){
Account account = new Account();- account.withdraw(0);
- String statement = account.getStatement();
- assertEquals("0", statement);
- }
- @Test
- void withdraw_reduces_balance_by_amount_withdrawn(){
Account account = new Account();- account.deposit(500);
- account.withdraw(100);
- String statement = account.getStatement();
- assertEquals("400", statement);
- }
- @Test
- void cannot_withdraw_more_than_you_have() {
Account account = new Account();- account.deposit(100);
- assertThrows(IllegalArgumentException.class, () -> { account.withdraw(200); });
- }
- @Test
- void cannot_withdraw_negative_money() {
Account account = new Account();- assertThrows(IllegalArgumentException.class, () -> { account.withdraw(-100); });
- }
- @Test
- void cannot_deposit_negative_money() {
- assertThrows(IllegalArgumentException.class, () -> { account.deposit(-100); });
- }
- }
Your Task
Your bank is tired of its mainframe COBOL accounting software and they hired both of you for a greenfield project in - what a happy coincidence - your favorite programming language!
Your task is to show them that your TDD-fu and your new-age programming language can cope with good ole’ COBOL!
Requirements
Write a class Account that offers the following methods:
void deposit(int);
void withdraw(int) throws IllegalArgumentException;
String printStatement();
An example statement would be:
Date | Amount | Balance |
---|---|---|
24.12.2015 | +500 | 500 |
23.8.2016 | -100 | 400 |
class Account { private int balance = 0; void deposit(int amount){ balance += amount; } void withdraw(int amount) throws IllegalArgumentException{ if(amount > balance) { throw new IllegalArgumentException("You cannot withdraw more than you have!"); } balance -= amount ; } String getStatement() { return String.valueOf(balance); } }
- class Account {
- private int balance = 0;
- void deposit(int amount){
- balance += amount;
- }
void withdraw(int amount){- void withdraw(int amount) throws IllegalArgumentException{
- if(amount > balance) {
- throw new IllegalArgumentException("You cannot withdraw more than you have!");
- }
- balance -= amount ;
- }
- String getStatement() {
return "" + balance; //Integer.toString(balance); //- return String.valueOf(balance);
- }
- }
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; class AccountTest { @Test void deposit_adds_money_to_initial_balance() { Account account = new Account(); account.deposit(100); String statement = account.getStatement(); assertEquals("100", statement); } @Test void deposit_adds_different_amount_of_money_to_initial_balance(){ Account account = new Account(); account.deposit(200); String statement = account.getStatement(); assertEquals("200", statement); } @Test void withdraw_of_zero_balance_remains_same(){ Account account = new Account(); account.withdraw(0); String statement = account.getStatement(); assertEquals("0", statement); } @Test void withdraw_reduces_balance_by_amount_withdrawn(){ Account account = new Account(); account.deposit(500); account.withdraw(100); String statement = account.getStatement(); assertEquals("400", statement); } @Test void cannot_withdraw_more_than_you_have() { Account account = new Account(); account.deposit(100); assertThrows(IllegalArgumentException.class, () -> { account.withdraw(200); }); } }
- import org.junit.jupiter.api.Test;
- import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;// TODO: Replace examples and use TDD by writing your own tests- import static org.junit.jupiter.api.Assertions.assertThrows;
- class AccountTest {
@Testvoid deposit_adds_money_to_initial_balance() {Account account = new Account();account.deposit(100);String statement = account.getStatement();assertTrue(statement.contains("100"));}- @Test
- void deposit_adds_money_to_initial_balance() {
- Account account = new Account();
- account.deposit(100);
- String statement = account.getStatement();
- assertEquals("100", statement);
- }
@Testvoid deposit_adds_different_amount_of_money_to_initial_balance(){Account account = new Account();account.deposit(200);String statement = account.getStatement();assertTrue(statement.contains("200"));}- @Test
- void deposit_adds_different_amount_of_money_to_initial_balance(){
- Account account = new Account();
- account.deposit(200);
- String statement = account.getStatement();
- assertEquals("200", statement);
- }
- @Test
- void withdraw_of_zero_balance_remains_same(){
- Account account = new Account();
account.withdraw(0);String statement = account.getStatement();assertTrue(statement.contains("0"));- account.withdraw(0);
- String statement = account.getStatement();
- assertEquals("0", statement);
- }
- @Test
- void withdraw_reduces_balance_by_amount_withdrawn(){
- Account account = new Account();
account.deposit(500);account.withdraw(100);String statement = account.getStatement();assertTrue(statement.contains("400"));- account.deposit(500);
- account.withdraw(100);
- String statement = account.getStatement();
- assertEquals("400", statement);
- }
}- @Test
- void cannot_withdraw_more_than_you_have() {
- Account account = new Account();
- account.deposit(100);
- assertThrows(IllegalArgumentException.class, () -> { account.withdraw(200); });
- }
- }