Ad

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)
Code
Diff
  • 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);
    • }
    • }

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
Code
Diff
  • 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);
    • }
    • }