DEV Community

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

Collapse
 
aspittel profile image
Ali Spittel

Tuesday - Don't give me five! (7 KYU)

In this kata you get the start number and the end number of a region and should return the count of all numbers except numbers with a 5 in it. The start and the end number are both inclusive!

codewars.com/kata/dont-give-me-five

Collapse
 
sdicke profile image
Sebastian Martin Dicke • Edited

Haskell

-- The first line is not necessary
dont_give_me_five :: [Int] -> Int
dont_give_me_five list = sum $ filter (\x -> notElem '5' $ show x) list
Collapse
 
sdicke profile image
Sebastian Martin Dicke • Edited

Another Haskell solution:

dont_give_me_five :: [Int] -> Int
dont_give_me_five = foldl (\x y -> if notElem '5' $ show y then x + y else x) 0

Or a little bit shorter in the same language:

dont_give_me_five :: [Int] -> Int
dont_give_me_five = foldl1 (\x y -> if notElem '5' $ show y then x + y else x)
Collapse
 
ben profile image
Ben Halpern • Edited

Ruby

def dont_give_me_five(start, end)
  (start..end).to_a.reject { |n| n.to_s.include? "5" }.size
end
Collapse
 
jay profile image
Jay

Rust Solution:

fn dont_give_me_5(start: usize, end: usize) -> u8 {
    (start..=end)
        .map(|n| n.to_string())
        .filter(|n| !n.contains('5'))
        .count() as u8
}
Collapse
 
ihate1999 profile image
Abubakar Nur Khalil • Edited

Quick JS solution

const dgmf = (init, lim) => {
    let count = 0

    for (var i = init-1;i < lim+1;i++) {
        count = String(i).includes('5') ? count : count + 1
        // count = i % 5 != 0 ? count + 1 : count
    }

    return count
}
Collapse
 
theluk profile image
Lukas Klinzing

This removes more than 5s

Thread Thread
 
ihate1999 profile image
Abubakar Nur Khalil

True, just realized 😂

Changing the conditional expression should fix that

// ...
// Loop code
count = String(i).includes('5') ? count : count + 1
Collapse
 
wodin profile image
Michael Wood

Clojure

(defn no5? [num]
  (not-any? #{\5} (str num)))

(defn dont-give-me-five! [start end]
  (->>
    (range start (inc end))
    (filter no5?)
    count))

user=> (dont-give-me-five! 1 9)
8
user=> (dont-give-me-five! 4 17)
12
Collapse
 
gypsydave5 profile image
David Wickes

Common Lisp

(defun dont-give-me-five (start end)
  (loop for x from start to end
     when (not (contains-5-p x)) collect x))

(defun contains-5-p (n)
  (position #\5 (write-to-string n)))
Collapse
 
cookavich profile image
Paul Cook

Another JS solution:

const dontGiveMeFive = (start, end) => range(start, end).filter(noFive).length;
const range = (start, end) => [...Array(end - start + 1).keys()].map(i => i + start);
const noFive = num => !`${num}`.includes('5');
Collapse
 
choroba profile image
E. Choroba

Perl solution, test included:

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

sub count_without_5 {
    my ($from, $to) = @_;
    return scalar grep $_ !~ /5/, $from .. $to
}

use Test::More tests => 2;
is count_without_5(1,9), 8;
is count_without_5(4,17), 12;
Collapse
 
kungtotte profile image
Thomas Landin

Here's my Nim solution :)

There are filter and keepIf functions defined in the sequtils standard library but that feels like cheating :P

$ is Nim's stringify operator, and the find procedure returns -1 if it doesn't find the substring :)

import strutils # for the 'find' procedure

proc my_filter(start, stop: int): seq[int] =
  for i in start..stop:
    if find($i, "5") == -1:
      result.add(i)

echo my_filter(1,9).len
echo my_filter(4,17).len
Collapse
 
aspittel profile image
Ali Spittel
def dont_give_me_five(start, end):
    return len([n for n in range(start, end + 1) if '5' not in str(n)])
Collapse
 
pulljosh profile image
Josh Pullen

Any application that can be written in JavaScript, will eventually be written in JavaScript:

const dontGiveMeFive = (min, max) =>
  [...Array(max - min + 1).keys()]
  .map(n => n + min)
  .filter(n => !`${n}`.includes('5'))
Collapse
 
jdsteinhauser profile image
Jason Steinhauser

Elixir:

no_fives = fn x, y -> x..y |> Enum.map(& Integer.to_string(&1)) |> Enum.filter(& !String.contains?(&1, "5")) |> Enum.count() end

And surprise! I wrote an F# solution in a similar fashion:

let no_fives x y =
  [x .. y]
  |> Seq.map string
  |> Seq.filter (fun x -> x.Contains("5") |> not)
  |> Seq.length
Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
jessicacastro profile image
Jessica Castro • Edited

Cool solution! But this will timeout with large numbers!