DEV Community

Daily Challenge #69 - Going to the Cinema

dev.to staff on September 17, 2019

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-effecti...
Collapse
 
tpolajnar profile image
TadPol

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)
Collapse
 
qm3ster profile image
Mihail Malo • Edited

"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
}
Collapse
 
yas46 profile image
Yasser Beyer

denied on first lol

Collapse
 
qm3ster profile image
Mihail Malo

Check timestamp by hovering the date lol

Collapse
 
aminnairi profile image
Amin • Edited

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
        ]
Collapse
 
choroba profile image
E. Choroba • Edited
#!/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;
Collapse
 
yas46 profile image
Yasser Beyer • Edited

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;
}