Skip to content

Instantly share code, notes, and snippets.

@muinjs
Forked from lbvf50mobile/count_tiny_parts.md
Created August 28, 2020 19:50
Show Gist options
  • Select an option

  • Save muinjs/8c44c2419b546a88af114442ecd73027 to your computer and use it in GitHub Desktop.

Select an option

Save muinjs/8c44c2419b546a88af114442ecd73027 to your computer and use it in GitHub Desktop.
tetst.rb

countTinyPairs ( 0:55:11 ) Codewriting

You are given two arrays of integers a and b of the same length, and an integer k. We will be iterating through array a from left to right, and simultaneously through array b from right to left, and looking at pairs (x, y), where x is from a and y is from b. Such a pair is called tiny if the concatenation xy is strictly less than k.

Your task is to return the number of tiny pairs that you'll encounter during the simultaneous iteration through a and b.

Example

For a = [1, 2, 3], b = [1, 2, 3], and k = 31, the output should be countTinyPairs(a, b, k) = 2.

We're considering the following pairs during iteration:

(1, 3). Their concatenation equals 13, which is less than 31, so the pair is tiny; (2, 2). Their concatenation equals 22, which is less than 31, so the pair is tiny; (3, 1). Their concatenation equals 31, which is not less than 31, so the pair is not tiny. As you can see, there are 2 tiny pairs during the iteration, so the answer is 2.

For a = [16, 1, 4, 2, 14], b = [7, 11, 2, 0, 15], and k = 743, the output should be countTinyPairs(a, b, k) = 4.

We're considering the following pairs during iteration:

(16, 15). Their concatenation equals 1615, which is greater than 743, so the pair is not tiny; (1, 0). Their concatenation equals 10, which is less than 743, so the pair is tiny; (4, 2). Their concatenation equals 42, which is less than 743, so the pair is tiny. (2, 11). Their concatenation equals 211, which is less than 743, so the pair is tiny; (14, 7). Their concatenation equals 147, which is less than 743, so the pair is tiny. There are 4 tiny pairs during the iteration, so the answer is 4

def countTinyPairs(a, b, k)
a.zip(b.reverse).count{|(x,y)| "#{x}#{y}".to_i < k}
end

You've created a new programming language, and now you've decided to add hashmap support to it. Actually you are quite disappointed that in common programming languages it's impossible to add a number to all hashmap keys, or all its values. So you've decided to take matters into your own hands and implement your own hashmap in your new language that has the following operations:

insert x y - insert an object with key x and value y. get x - return the value of an object with key x. addToKey x - add x to all keys in map. addToValue y - add y to all values in map. To test out your new hashmap, you have a list of queries in the form of two arrays: queryTypes contains the names of the methods to be called (eg: insert, get, etc), and queries contains the arguments for those methods (the x and y values).

Your task is to implement this hashmap, apply the given queries, and to find the sum of all the results for get operations.

def hashMap(queryType, a)
h = {}
ans = 0
(0...a.size).each do |i|
if "insert" == queryType[i]
h[a[i][0]] = a[i][1]
elsif "get" == queryType[i]
ans += h[a[i][0]]
elsif "addToKey" == queryType[i]
h.transform_keys!{|k| k+a[i][0]}
else
h.keys.each do |j|
h[j] += a[i][0]
end
end
end
ans
end
def hashMap(queryType, query)
h = {}
ans = 0
actions = {
"insert" => ->a{ h[a[0]] = a[1]},
"get" => ->a{ ans += h[a[0]]; h[a[0]]},
"addToKey" => ->a{
h.transform_keys!{|k| k+a[0]}
},
"addToValue" => ->a{
h.keys.each do |i|
h[i] += a[0]
end
}
}
queryType.zip(query).each do |q,a|
actions[q][a]
end
ans
end
def mutateTheArray(n, a)
prv = 0
(0...a.size).each do |i|
nxt = i+1 == a.size ? 0 : a[i+1]
value = prv + a[i] + nxt
prv = a[i]
a[i] = value
end
a
end
def mutateTheArray(n, a)
b = a.clone
(0...b.size).each do |i|
prev = i == 0 ? 0 : a[i-1]
nxt = i == n-1 ? 0 : a[i+1]
b[i] = a[i] + prev + nxt
end
(0...a.size).each do |i|
a[i] = b[i]
end
a
end

Given an integer n and an array a of length n, your task is to apply the following mutation to a:

Array a mutates into a new array b of length n. For each i from 0 to n - 1, b[i] = a[i - 1] + a[i] + a[i + 1]. If some element in the sum a[i - 1] + a[i] + a[i + 1] does not exist, it should be set to 0. For example, b[0] should be equal to 0 + a[0] + a[1]. Example

For n = 5 and a = [4, 0, 1, -2, 3], the output should be mutateTheArray(n, a) = [4, 5, -1, 2, 1].

b[0] = 0 + a[0] + a[1] = 0 + 4 + 0 = 4 b[1] = a[0] + a[1] + a[2] = 4 + 0 + 1 = 5 b[2] = a[1] + a[2] + a[3] = 0 + 1 + (-2) = -1 b[3] = a[2] + a[3] + a[4] = 1 + (-2) + 3 = 2 b[4] = a[3] + a[4] + 0 = (-2) + 3 + 0 = 1 So, the resulting array after the mutation will be [4, 5, -1, 2, 1].

Codewriting

You are implementing your own programming language and you've decided to add support for merging strings. A typical merge function would take two strings s1 and s2, and return the lexicographically smallest result that can be obtained by placing the symbols of s2 between the symbols of s1 in such a way that maintains the relative order of the characters in each string.

For example, if s1 = "super" and s2 = "tower", the result should be merge(s1, s2) = "stouperwer". You'd like to make your language more unique, so for your merge function, instead of comparing the characters in the usual lexicographical order, you'll compare them based on how many times they occur in their respective strings (fewer occurrences means the character is considered smaller). If the number of occurrences are equal, then the characters should be compared in the usual way. If both number of occurences and characters are equal, you should take the characters from the first string to the result.

Given two strings s1 and s2, return the result of the special merge function you are implementing.

def mergeStrings(s1, s2)
a = s1.chars
b = s2.chars
ha = a.each_with_object(Hash.new){|v,obj| obj[v] ||=0; obj[v]+=1}
hb = b.each_with_object(Hash.new){|v,obj| obj[v] ||=0; obj[v]+=1}
ans = ""
p "Words are #{s1} and #{s2}"
while (!a.empty?) || (!b.empty?)
x,y = !a.empty? , !b.empty?
if x && y
if ha[a[0]] < hb[b[0]]
ans += a[0]
a.shift
elsif ha[a[0]] > hb[b[0]]
ans += b[0]
b.shift
elsif a[0] <= b[0]
ans += a[0]
a.shift
else
ans += b[0]
b.shift
end
elsif !x
ans += b[0]
b.shift
else
ans += a[0]
a.shift
end
end
ans
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment