DEV Community

Discussion on: Daily Coding Puzzles - Nov 11th - Nov 16th

Collapse
 
aspittel profile image
Ali Spittel

Monday - Transportation on vacation (8 KYU):

You will need a rental car in order for you to get around in your vacation. The manager of the car rental makes you some good offers.

codewars.com/kata/transportation-o...

Collapse
 
sdicke profile image
Sebastian Martin Dicke • Edited

Haskell solution:

transportation_on_vacation :: Int -> Int
transportation_on_vacation days
    | days > 6 = max_costs - 50
    | days > 3 = max_costs - 20
    | otherwise = max_costs
    where
    max_costs = 40 * days 
Collapse
 
ben profile image
Ben Halpern

Ruby

def rental_price(num_days)
  price = num_days * 40
  return price - 50 if num_days >= 7
  return price - 20 if num_days >= 3
  price
end
Collapse
 
choroba profile image
E. Choroba

Perl solution, tests included:

#! /usr/bin/perl
use warnings;
use strict;

my @discounts = ([7, 50], [3, 20], [0, 0]);
sub total {
    my ($days) = @_;
    my $total = $days * 40;
    for (@discounts) {
        my ($at_least, $discount) = @$_;
        return $total - $discount if $days >= $at_least;
    }
}

use Test::More tests => 8;
is total(1), 40;
is total(2), 80;
is total(3), 3 * 40 - 20;
is total(4), 4 * 40 - 20;
is total(5), 5 * 40 - 20;
is total(6), 6 * 40 - 20;
is total(7), 7 * 40 - 50;
is total(8), 8 * 40 - 50;
Collapse
 
kungtotte profile image
Thomas Landin

My solution in Nim, my new favourite language :)

It provides an automatic default return variable called "result" that you can just start stuffing things into and it will automatically return it without you doing anything :)

let
  rate = 40.0
  weekly_discount = 50.0
  weekend_discount = 20.0
  test_data = [1, 2, 3, 4, 5, 6, 7, 8, 9]

proc total_cost(days: int): float =
  result = days.float * rate
  if days >= 7:
    result -= weekly_discount
  elif days >= 3:
    result -= weekend_discount

for n in test_data:
  echo "Cost for ", n, " days is: ", total_cost(n)
Collapse
 
jay profile image
Jay

I try to use the func keyword when defining methods, that way the compiler can guarantee that I have pure function with no side effects.
func is just sugar for proc {.noSideEffect.}.

func rental_car_cost(days: int): int =
  result = days * 40
  if days > 7:
    result -= 50
  elif days >= 3:
    result -= 20
Thread Thread
 
kungtotte profile image
Thomas Landin

I haven't gotten into the habit of doing that yet. I know I should and I think it's a great idea in general, but years of habits are hard to break and the func keyword is still a fairly recent addition to the language :)

Collapse
 
aspittel profile image
Ali Spittel

Java (!!) solution

public class Kata {
  public static int rentalCarCost(int d) {
    int total = d * 40;
    if(d >= 7)
      total -= 50;
    else if(d >= 3)
      total -= 20;
    return total;
  }
}
Collapse
 
jay profile image
Jay

Rust Solution:

fn rental_car_cost(days: i32) -> i32 {
    (days * 40) - match days {
        n if n > 7 => 50,
        n if n >= 3 => 20,
        _ => 0,
    }
}
Collapse
 
clandau profile image
Courtney • Edited
function rentalCarCost(d : number) : number {
    let discount : number = 0;
    if(d >= 7) discount = 50;
    else if(d >= 3) discount = 20;
    return (d * 40) - discount;
}

//one-liner
function rentalCarCost2(d : number) : number {
    return d >= 7 ? ((d * 40) - 50) : (d >= 3 ? (d * 40) - 20 : d * 40);
}