#72 Moving Zeros To The End


Write an algorithm that takes an array and moves all of the zeros to the end, preserving the order of the other elements.


moveZeros([false,1,0,1,2,0,1,3,"a"]) // returns[false,1,1,2,1,3,"a",0,0]


 #69  Weight for weight


My friend John and I are members of the "Fat to Fit Club (FFC)". John is worried because each month a list with the weights of members is published and each month he is the last on the list which means he is the heaviest.


I am the one who establishes the list so I told him: "Don't worry any more, I will modify the order of the list". It was decided to attribute a "weight" to numbers. The weight of a number will be from now on the sum of its digits.


For example 99 will have "weight" 18, 100 will have "weight" 1 so in the list 100 will come before 99. Given a string with the weights of FFC members in normal order can you give this string ordered by "weights" of these numbers?


Example:

"56 65 74 100 99 68 86 180 90" ordered by numbers weights becomes: "100 180 90 56 65 74 68 86 99"


When two numbers have the same "weight", let us class them as if they were strings and not numbers: 100 is before 180 because its "weight" (1) is less than the one of 180 (9) and 180 is before 90 since, having the same "weight" (9) it comes before as a string.


All numbers in the list are positive numbers and the list can be empty.




 #63 Sort two arrays


When no more interesting kata can be resolved, I just choose to create the new kata, to solve their own, to enjoy the process --myjinxin2015 said


Description:

Give you two arrays arr1 and arr2. They have the same length(length>=2). The elements of two arrays always be integer.


Sort arr1 according to the ascending order of arr2; Sort arr2 according to the ascending order of arr1. Description is not easy to understand, for example:


 arr1=[5,4,3,2,1], 

 arr2=[6,5,7,8,9]

Let us try to sorting arr1. First, we need know the ascending order of arr2:


 [5,6,7,8,9]

We can see, after sort arr2 to ascending order, some elements' index are changed:


 unsort arr2      ascending order arr2

 [6,5,7,8,9]--->      [5,6,7,8,9]


 index0(6)  --->      index1

 index1(5)  --->      index0

 index2(7)            index2(no change)

 index3(8)            index3(no change)

 index4(9)            index4(no change)

So, wo need according to these changes to sorting arr1:


 unsort arr1          sorted arr1

 [5,4,3,2,1]--->      [4,5,3,2,1]


 index0(5)  --->      index1

 index1(4)  --->      index0

 index2(3)            index2(no change)

 index3(2)            index3(no change)

 index4(1)            index4(no change)


 So: sorted arr1= [4,5,3,2,1]

And then, we sorting arr2 with the same process:


 unsort arr1      ascending order arr1

 [5,4,3,2,1]--->      [1,2,3,4,5]


 index0(5)  --->      index4

 index1(4)  --->      index3

 index2(3)            index2(no change)

 index3(2)  --->      index1

 index4(1)  --->      index0


 unsort arr2          sorted arr2

 [6,5,7,8,9]--->      [9,8,7,5,6]


 index0(6)  --->      index4

 index1(5)  --->      index3

 index2(7)            index2(no change)

 index3(8)  --->      index1

 index4(9)  --->      index0


 So: sorted arr2= [9,8,7,5,6]

Finally, returns the sorted arrays by a 2D array: [sorted arr1, sorted arr2]


Note: In ascending order sorting process(not the final sort), if some elements have same value, sort them according to their index; You can modify the original array, but I advise you not to do that. ;-)


Some Examples and explain

sortArrays([5,4,3,2,1],[6,5,7,8,9]) should return

[[4,5,3,2,1],[9,8,7,5,6]]


sortArrays([2,1,3,4,5],[5,6,7,8,9]) should return

[[2,1,3,4,5],[6,5,7,8,9]]


sortArrays([5,6,9,2,6,5],[3,6,7,4,8,1]) should return

[[5,5,2,6,9,6],[4,3,1,6,8,7]]




#50 String -> N iterations -> String



Welcome

This kata is inspired by This Kata


We have a string s


We have a number n


Here is a function that takes your string, concatenates the even-indexed chars to the front, odd-indexed chars to the back.


Examples

s = "Wow Example!"

result = "WwEapeo xml!"

s = "I'm JomoPipi"

result = "ImJm ii' ooPp"

The Task:

return the result of the string after applying the function to it n times.


example where s = "qwertyuio" and n = 2:


after 1 iteration  s = "qetuowryi"

after 2 iterations s = "qtorieuwy"

return "qtorieuwy"

Note

There are 50000 random tests where


the string contains between 50 and 200 chars

n is greater than a billion

so be ready to optimize.


#49 Playing with digits


Some numbers have funny properties. For example:


89 --> 8¹ + 9² = 89 * 1


695 --> 6² + 9³ + 5⁴= 1390 = 695 * 2


46288 --> 4³ + 6⁴+ 2⁵ + 8⁶ + 8⁷ = 2360688 = 46288 * 51


Given a positive integer n written as abcd... (a, b, c, d... being digits) and a positive integer p we want to find a positive integer k, if it exists, such as the sum of the digits of n taken to the successive powers of p is equal to k * n. In other words:


Is there an integer k such as : (a ^ p + b ^ (p+1) + c ^(p+2) + d ^ (p+3) + ...) = n * k


If it is the case we will return k, if not return -1.


Note: n, p will always be given as strictly positive integers.


digPow(89, 1) should return 1 since 8¹ + 9² = 89 = 89 * 1

digPow(92, 1) should return -1 since there is no k such as 9¹ + 2² equals 92 * k

digPow(695, 2) should return 2 since 6² + 9³ + 5⁴= 1390 = 695 * 2

digPow(46288, 3) should return 51 since 4³ + 6⁴+ 2⁵ + 8⁶ + 8⁷ = 2360688 = 46288 * 51



#48 A Rule of Divisibility by 13


When you divide the successive powers of 10 by 13 you get the following remainders of the integer divisions:


1, 10, 9, 12, 3, 4.


Then the whole pattern repeats.


Hence the following method: Multiply the right most digit of the number with the left most number in the sequence shown above, the second right most digit to the second left most digit of the number in the sequence. The cycle goes on and you sum all these products. Repeat this process until the sequence of sums is stationary.


...........................................................................


Example: What is the remainder when 1234567 is divided by 13?


7×1 + 6×10 + 5×9 + 4×12 + 3×3 + 2×4 + 1×1 = 178


We repeat the process with 178:


8x1 + 7x10 + 1x9 = 87


and again with 87:


7x1 + 8x10 = 87


...........................................................................


From now on the sequence is stationary and the remainder of 1234567 by 13 is the same as the remainder of 87 by 13: 9


Call thirt the function which processes this sequence of operations on an integer n (>=0). thirt will return the stationary number.


thirt(1234567) calculates 178, then 87, then 87 and returns 87.


thirt(321) calculates 48, 48 and returns 48




 #45 Sort - one, three, two

Hey You !

Sort these integers for me ...


By name ...


Do it now !


Input

Range is 0-999


There may be duplicates


The array may be empty


 #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]



#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).


+ Recent posts