loading...

Daily Challenge #69 - Going to the Cinema

thepracticaldev profile image dev.to staff ・1 min read

Today's challenge is to help John make an important financial decision!

John likes to go to the cinema, but he wants to find the most cost-effective way to go. He can buy a ticket for $15, or he can buy a membership card for $500. Every time he uses the membership card, the ticket will be 0.9 times the price he paid for the last one.

Ex. If John goes to the cinema three times:
A : 15 * 3 = 45
B : 500 + 15 * 0.90 + (15 * 0.90) * 0.90 + (15 * 0.90 * 0.90) * 0.90 ( = 536.5849999999999, no rounding for each ticket)

Create a function movie with three parameters: card (price of the card), ticket(normal price of a ticket), perc(fraction of what he paid for the previous ticket) and returns the first n such that ceil(price of B) < price of A

More examples:
movie(500, 15, 0.9)should return43
(with card the total price is 634, with tickets 645)

movie(100, 10, 0.95) should return 24
(with card the total price is 235, with tickets 240)

Good luck!


Today's challenge comes from CodeWars user g964. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

Posted on by:

thepracticaldev profile

dev.to staff

@thepracticaldev

The hardworking team behind dev.to ❤️

Discussion

pic
Editor guide
 

Here is my try:

function movie(card, ticket,perc){
    let i = 0
    let ticketFullPrice = 0
    let lowedTicket = ticket

    while(card>ticketFullPrice){
        ticketFullPrice += ticket
        lowedTicket *= perc 
        card += lowedTicket 
        i++
    }
    return i
}
movie(500,15,0.9)
movie(100, 10, 0.95)
 

"first" lmao

const movie = (card, ticket, part) => {
  let i = 0
  let a = 0
  let b = card
  do {
    a += ticket
    b += ticket * part ** i
    i++
  } while (Math.ceil(b) >= a)
  return i - 1
}

an "optimization" (no power)

const movie = (card, ticket, part) => {
  let i = 0
  let a = 0
  let b = card
  let cheap = ticket
  while (true) {
    a += ticket
    b += cheap
    cheap *= part
    if (Math.ceil(b) < a) return i
    i++
  }
}

"optimization II" (no Math.ceil, comparison with 0 instead of 1)

const movie = (card, ticket, part) => {
  let i = 0
  let dif = -card - 1
  let cheap = ticket
  while (true) {
    dif += ticket - cheap
    cheap *= part
    if (dif > 0) return i
    i++
  }
}

idiocy.

const movie = (card, ticket, part) => {
  let i = 1
  let dif = -card - 1
  let cheap = ticket
  while ((dif += ticket - (cheap *= part)) <= 0) i++
  return i
}
 

denied on first lol

 

Check timestamp by hovering the date lol

 

Elm

module Movie exposing (movie)


getReduction : Int -> Float -> Float
getReduction times percentage =
    percentage ^ toFloat times


profitableNumberOfMovies : Int -> Int -> Float -> Int -> Int
profitableNumberOfMovies card ticket percentage times =
    let
        reduction : Float
        reduction =
            getReduction times percentage

        increasedCard : Int
        increasedCard =
            ceiling <| toFloat card + toFloat ticket * reduction

        increasedTicket : Int
        increasedTicket =
            ticket * times
    in
    if increasedCard < increasedTicket then
        times - 1

    else
        profitableNumberOfMovies increasedCard ticket percentage <| times + 1


movie : Int -> Int -> Float -> Int
movie card ticket percentage =
    profitableNumberOfMovies card ticket percentage 1

Tests

module MovieTest exposing (suite)

import Expect
import Movie exposing (movie)
import Test exposing (Test)


suite : Test
suite =
    Test.describe "Movie"
        [ Test.test "It should return 43 for movie 500 15 0.9" <|
            \_ ->
                Expect.equal 43 <| movie 500 15 0.9
        , Test.test "It should return 24 for movie 100 10 0.95" <|
            \_ ->
                Expect.equal 24 <| movie 100 10 0.95
        ]
 
#!/usr/bin/perl
use warnings;
use strict;

sub movie {
    my ($card, $ticket, $perc) = @_;
    my $reduced = my $sum = $ticket * $perc;
    my $count = 0;
    $sum += ($reduced *= $perc)
        while $ticket * ++$count < $card + int $sum;
    return $count
}

use Test::More tests => 3;

is movie(100, 200, 0.5),   1;
is movie(500,  15, 0.9),  43;
is movie(100,  10, 0.95), 24;
 

JavaScript

const movie = (card, ticket, perc) => {
    let n = 1;
    let frac = ticket * perc;
    let a = ticket * n;
    let b = card + frac;
    while(a < Math.ceil(b)) {
        n++;
        frac = frac * perc;
        b = b + frac;
        a = ticket * n;
    }
    return n;
}