#57 Frequency Analysis With Buckets


Description:


Given an input array (arr) of positive integers, the objective is to return an output array where each index represents the amount of times an element appeared (frequency) in the input array.


More specifically, the element at each index of the output array will be an array (bucket) containing integers that appeared index-amount-of-times.


Otherwise, slot nulls (JavaScript, Java), nils (Ruby), or NULL's (C/C++) where appropriate. A valid array will always be provided.


If an array of [1,2,3,4,4,5,5,5] is passed in, the expected output should be: [null, [1,2,3], [4], [5], null, null, null, null, null].


Explanation:


// bucketize(arr) ======> outputArray

bucketize({1,2,3,4,4,5,5,5}) ======> {null, {1,2,3}, {4}, {5}, null, null, null, null, null}

An element cannot appear 0 times, so a null is placed at outputArray[0]. The elements 1, 2, and 3 appear once. This is why they are located at outputArray[1]. Notice the elements are grouped together in an array and sorted in ascending order. The element 4 appears twice. This is why it is located at outputArray[2]. The element 5 appears three times. This is why it is located at outputArray[3].


Although an integer could have possibly appeared four, five, six, seven, or eight times, this is not the case for this particular example. This is the reason why the elements at outputArray[4], outputArray[5], outputArray[6], outputArray[7], and outputArray[8] are all null values.


Examples:


bucketize({2,2,4,4,6,6,9,9,9,9}) ===> {null, null, {2,4,6}, null, {9}, null, null, null, null, null, null};

bucketize({3,3,3,3,2}) =============> {null, {2}, null, null, {3}, null};

bucketize({5,5,5,5,5}) =============> {null, null, null, null, null, {5}};

bucketize({77,3,40,40,40}) =========> {null, {3,77}, null, {40}, null, null};

bucketize({16,7,5,3,6,23}) =========> {null, {3,5,6,7,16,23}, null, null, null, null, null};



 #56 Maximum subarray sum


The maximum sum subarray problem consists in finding the maximum sum of a contiguous subsequence in an array or list of integers:


Max.sequence(new int[]{-2, 1, -3, 4, -1, 2, 1, -5, 4});

// should be 6: {4, -1, 2, 1}

Easy case is when the list is made up of only positive numbers and the maximum sum is the sum of the whole array. If the list is made up of only negative numbers, return 0 instead.


Empty list is considered to have zero greatest sum. Note that the empty list or array is also a valid sublist/subarray.



#55 Sum of Two Integers



Task

Given Two intgers a , b , find The sum of them , BUT You are not allowed to use the operators + and -


Notes

The numbers (a,b) may be positive , negative values or zeros .


Returning value will be integer .


Javascript: the Array reduce methods are disabled, along with eval, require, and module .


Java: the following methods are prohibited: addExact, average, collect, decrement, increment, nextAfter, nextDown, nextUp, reduce, subtractExact, sum, summing . The following classes are prohibited: BigDecimal and BigInteger .


NASM: the following instructions are prohibited: add, adc, adcx, adox, dec, inc, sbb, sub .


Input >> Output Examples

1- Add (5,19) ==> return (24) 


2- Add (-27,18) ==> return (-9)


3- Add (-14,-16) ==> return (-30)



#54 Word a10n (abbreviation)



The word i18n is a common abbreviation of internationalization in the developer community, used instead of typing the whole word and trying to spell it correctly. Similarly, a11y is an abbreviation of accessibility.


Write a function that takes a string and turns any and all "words" (see below) within that string of length 4 or greater into an abbreviation, following these rules:


A "word" is a sequence of alphabetical characters. By this definition, any other character like a space or hyphen (eg. "elephant-ride") will split up a series of letters into two words (eg. "elephant" and "ride").

The abbreviated version of the word should have the first letter, then the number of removed characters, then the last letter (eg. "elephant ride" => "e6t r2e").


Example

abbreviate("elephant-rides are really fun!")

//          ^^^^^^^^*^^^^^*^^^*^^^^^^*^^^*

// words (^):   "elephant" "rides" "are" "really" "fun"

//                123456     123     1     1234     1

// ignore short words:               X              X


// abbreviate:    "e6t"     "r3s"  "are"  "r4y"   "fun"

// all non-word characters (*) remain in place

//                     "-"      " "    " "     " "     "!"

=== "e6t-r3s are r4y fun!"



#53 FIXME: Hello


The code provided has a method hello which is supposed to show only those attributes which have been explicitly set. Furthermore, it is supposed to say them in the same order they were set.


But it's not working properly.


Notes

There are 3 attributes


name

age

sex ('M' or 'F')

When the same attribute is assigned multiple times the hello method shows it only once. If this happens the order depends on the first assignment of that attribute, but the value is from the last assignment.


Examples

Hello.

Hello. My name is Bob. I am 27. I am male.

Hello. I am 27. I am male. My name is Bob.

Hello. My name is Alice. I am female.

Hello. My name is Batman.




#52 Are they the "same"?


Given two arrays a and b write a function comp(a, b) (compSame(a, b) in Clojure) that checks whether the two arrays have the "same" elements, with the same multiplicities. "Same" means, here, that the elements in b are the elements in a squared, regardless of the order.


Examples

Valid arrays

a = [121, 144, 19, 161, 19, 144, 19, 11]  

b = [121, 14641, 20736, 361, 25921, 361, 20736, 361]

comp(a, b) returns true because in b 121 is the square of 11, 14641 is the square of 121, 20736 the square of 144, 361 the square of 19, 25921 the square of 161, and so on. It gets obvious if we write b's elements in terms of squares:


a = [121, 144, 19, 161, 19, 144, 19, 11] 

b = [11*11, 121*121, 144*144, 19*19, 161*161, 19*19, 144*144, 19*19]

Invalid arrays

If we change the first number to something else, comp may not return true anymore:


a = [121, 144, 19, 161, 19, 144, 19, 11]  

b = [132, 14641, 20736, 361, 25921, 361, 20736, 361]

comp(a,b) returns false because in b 132 is not the square of any number of a.


a = [121, 144, 19, 161, 19, 144, 19, 11]  

b = [121, 14641, 20736, 36100, 25921, 361, 20736, 361]

comp(a,b) returns false because in b 36100 is not the square of any number of a.



#51 Simple string indices


n this Kata, you will be given a string with brackets and an index of an opening bracket and your task will be to return the index of the matching closing bracket. Both the input and returned index are 0-based except in Fortran where it is 1-based. An opening brace will always have a closing brace. Return -1 if there is no answer (Haskell return Nothing, Fortran: return 0)


For example


solve("((1)23(45))(aB)", 0) = 10 // the opening brace at index 0 matches the closing brace at index 10

solve("((1)23(45))(aB)", 1) = 3 

solve("((1)23(45))(aB)", 2) = -1 // there is no opening bracket at index 2, so return -1

solve("((1)23(45))(aB)", 6) = 9

solve("((1)23(45))(aB)", 11) = 14

solve("((>)|?(*'))(yZ)", 11) = 14



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


각 데이터베이스별 문자열은 연결하는 함수 차이

MySQL: CONCAT( )

Oracle: CONCAT( ), ||

SQL Server: +


#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



+ Recent posts