#44 Bouncing Balls


A child plays with a ball on the nth floor of a big building. The height of this floor is known.


(float parameter "h" in meters. Condition 1) : h > 0)


He lets out the ball. The ball bounces for example to two-thirds of its height.


(float parameter "bounce". Condition 2) : 0 < bounce < 1)


His mother looks out of a window that is 1.5 meters from the ground.


(float parameters "window". Condition 3) : window < h).


How many times will the mother see the ball either falling or bouncing in front of the window?


If all three conditions above are fulfilled, return a positive integer, otherwise return -1.


Note


You will admit that the ball can only be seen if the height of the rebouncing ball is stricty greater than the window parameter.


Example:


h = 3, bounce = 0.66, window = 1.5, result is 3


h = 3, bounce = 1, window = 1.5, result is -1 (Condition 2) not fulfilled).



 #43Counting Duplicates


Count the number of Duplicates

Write a function that will return the count of distinct case-insensitive alphabetic characters and numeric digits that occur more than once in the input string. The input string can be assumed to contain only alphabets (both uppercase and lowercase) and numeric digits.


Example

"abcde" -> 0 # no characters repeats more than once

"aabbcde" -> 2 # 'a' and 'b'

"aabBcde" -> 2 # 'a' occurs twice and 'b' twice (bandB)

"indivisibility" -> 1 # 'i' occurs six times

"Indivisibilities" -> 2 # 'i' occurs seven times and 's' occurs twice

"aA11" -> 2 # 'a' and '1'

"ABBA" -> 2 # 'A' and 'B' each occur twice



'매일매일개발' 카테고리의 다른 글

#3 PANGRAM  (0) 2018.03.08
#2 가챠 - 가중치가 있는 랜덤  (0) 2018.03.07
#1 문자열 역순으로 출력하기  (0) 2018.03.07

#42 Delete occurrences of an element if it occurs more than n times



Enough is enough!

Alice and Bob were on a holiday. Both of them took many pictures of the places they've been, and now they want to show Charlie their entire collection. However, Charlie doesn't like this sessions, since the motive usually repeats. He isn't fond of seeing the Eiffel tower 40 times. He tells them that he will only sit during the session if they show the same motive at most N times. Luckily, Alice and Bob are able to encode the motive as a number. Can you help them to remove numbers such that their list contains each number only up to N times, without changing the order?


Task

Given a list lst and a number N, create a new list that contains each number of lst at most N times without reordering. For example if N = 2, and the input is [1,2,3,1,2,1,2,3], you take [1,2,3,1,2], drop the next [1,2] since this would lead to 1 and 2 being in the result 3 times, and then take 3, which leads to [1,2,3,1,2,3].


Example

EnoughIsEnough.deleteNth(new int[] {20,37,20,21}, 1) // return [20,37,21]

EnoughIsEnough.deleteNth(new int[] {1,1,3,3,7,2,2,2,2}, 3) // return [1, 1, 3, 3, 7, 2, 2, 2]



#41 Dashatize it

 

 

Given a number, return a string with dash'-'marks before and after each odd integer, but do not begin or end the string with a dash mark.

 

Ex:
dashatize(274) -> '2-7-4'
dashatize(6815) -> '68-1-5'

 

#40 What's a Perfect Power anyway?


A perfect power is a classification of positive integers:


In mathematics, a perfect power is a positive integer that can be expressed as an integer power of another positive integer. More formally, n is a perfect power if there exist natural numbers m > 1, and k > 1 such that mk = n.


Your task is to check wheter a given integer is a perfect power. If it is a perfect power, return a pair m and k with mk = n as a proof. Otherwise return Nothing, Nil, null, NULL, None or your language's equivalent.


Note: For a perfect power, there might be several pairs. For example 81 = 3^4 = 9^2, so (3,4) and (9,2) are valid solutions. However, the tests take care of this, so if a number is a perfect power, return any pair that proves it.


Examples

isPerfectPower(4) => new int[]{2,2}

isPerfectPower(5) => null

isPerfectPower(8) => new int[]{2,3}

isPerfectPower(9) => new int[]{3,2}


#39 Help the bookseller ! 


A bookseller has lots of books classified in 26 categories labeled A, B, ... Z. Each book has a code of 3, 4, 5 or more capitals letters. The first letter of a letter is the capital letter of the book category. In the bookseller's stocklist each code is followed by a space and a positive integer n (int n> = 0) which indicates the quantity of books in this code in stock.


For example an extract of one of the stocklists could be:


L = {"ABART 20", "CDXEF 50", "BKWRK 25", "BTSQZ 89", "DRTYM 60"}.

or


L = ["ABART 20", "CDXEF 50", "BKWRK 25", "BTSQZ 89", "DRTYM 60"] or ....

You will be given a stocklist (L) and a list of categories in capital letters.


  M = {"A", "B", "C", "W"}

or


  M = ["A", "B", "C", "W"] or ...

and your task is to find all the books of L with codes belonging to each category.


For the lists L and M, you have to return the string (in Haskell / Clojure a list of pairs):


  (A: 20) - (B: 114) - (C: 50) - (W: 0)

"BKWRK" and "BTSQZ", 50 corresponding to "CDXEF" and 0 to category 'W', where A, B, C and W are the categories, since there is no code beginning with W.


If L or M are empty return string is "" (Clojure should return an empty array instead).


#38  Highest Scoring Word


Given a string of words, you need to find the highest scoring word.


Each letter of a word scores points according to it's position in the alphabet: a = 1, b = 2, c = 3 etc.


You need to return the highest scoring word as a string.


If two words score the same, return the word that appears earliest in the original string.


All letters will be lowercase and all inputs will be valid.




 #37 Count the smiley faces!


Description:

Given an array (arr) as an argument complete the function countSmileys that should return the total number of smiling faces.


Rules for a smiling face:

-Each smiley face must contain a valid pair of eyes. Eyes can be marked as : or ;

-A smiley face can have a nose but it does not have to. Valid characters for a nose are - or ~

-Every smiling face must have a smiling mouth that should be marked with either ) or D.

No additional characters are allowed except for those mentioned.

Valid smiley face examples:

:) :D ;-D :~)

Invalid smiley faces:

;( :> :} :] 


Example cases:


countSmileys([':)', ';(', ';}', ':-D']);       // should return 2;

countSmileys([';D', ':-(', ':-)', ';~)']);     // should return 3;

countSmileys([';]', ':[', ';*', ':$', ';-D']); // should return 1;


Note: In case of an empty array return 0. You will not be tested with invalid input (input will always be an array). Order of the face (eyes, nose, mouth) elements will always be the same


Happy coding!


#36 The Supermarket Queue 


There is a queue for the self-checkout tills at the supermarket. Your task is write a function to calculate the total time required for all the customers to check out!


The function has two input variables:


customers: an array (list in python) of positive integers representing the queue. Each integer represents a customer, and its value is the amount of time they require to check out.

n: a positive integer, the number of checkout tills.

The function should return an integer, the total time required.


EDIT: A lot of people have been confused in the comments. To try to prevent any more confusion:


There is only ONE queue, and

The order of the queue NEVER changes, and

Assume that the front person in the queue (i.e. the first element in the array/list) proceeds to a till as soon as it becomes free.

The diagram on the wiki page I linked to at the bottom of the description may be useful.

So, for example:


queueTime([5,3,4], 1)

// should return 12

// because when n=1, the total time is just the sum of the times


queueTime([10,2,3,3], 2)

// should return 10

// because here n=2 and the 2nd, 3rd, and 4th people in the 

// queue finish before the 1st person has finished.


queueTime([2,3,10], 2)

// should return 12



 #35 Persistent Bugger.


Write a function, persistence, that takes in a positive parameter num and returns its multiplicative persistence, which is the number of times you must multiply the digits in num until you reach a single digit.


For example:


persistence(39) == 3 // because 3*9 = 27, 2*7 = 14, 1*4=4  and 4 has only one digit

persistence(999) == 4 // because 9*9*9 = 729, 7*2*9 = 126 ,1*2*6 = 12, and finally 1*2 = 2

persistence(4) == 0 // because 4 is already a one-digit number


+ Recent posts