Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Homework 2 #5

Open
JestAK opened this issue Nov 7, 2023 · 0 comments · Fixed by #3
Open

Homework 2 #5

JestAK opened this issue Nov 7, 2023 · 0 comments · Fixed by #3
Assignees
Labels
question Further information is requested

Comments

@JestAK
Copy link
Owner

JestAK commented Nov 7, 2023

First task

The Codewars Council meets at a circular table with n seats. Depending
on the day d of the month, d people will be chosen as leaders of the
council. These d leaders are spaced equidistantly from each other on the
table, like spokes on a wheel. The leaders are chosen based on which
grouping of d equidistant people have the largest summed honor. The
honor of the participants for that day in the Council is given in an
array arr of length n. Return the combined honor of the d leaders of the
Council.
Given conditions:
n % d == 0, ie. there will be no invalid cases. All groupings go fully
around the circle.
1 <= d <= 31, as you can't have 32 days in a month.
n > 0 because you can't have a Council of 0 people. (You also can't have
a table with 0 open seats, then it's just a nightstand or something)
You can have negative honor on Codewars (but it is hard to do) and the
solution may be negative.
Example:

[1, 2, 3, 4], 2 -> 6 because max(1+3, 2+4) == 6
[1, 5, 6, 3, 4, 2], 3 -> 11 because max(1+6+4, 5+3+2) == 11
[1, 1, 0], 1 -> 1 because max(1, 1, 0) == 1

Hint:
Something important to note is that you (usually) don't have to evaluate
every sum in the array, as after n/d repetitions the sums loop over on
themselves.

Second task

You are going to be given an array of integers. Your job is to take that
array and find an index N where the sum of the integers to the left of N
is equal to the sum of the integers to the right of N. If there is no
index that would make this happen, return -1.
For example:
Let's say you are given the array {1,2,3,4,3,2,1}:
Your function will return the index 3, because at the 3rd position of
the array, the sum of left side of the index ({1,2,3}) and the sum of
the right side of the index ({3,2,1}) both equal 6.
Let's look at another one.
You are given the array {1,100,50,-51,1,1}:
Your function will return the index 1, because at the 1st position of
the array, the sum of left side of the index ({1}) and the sum of the
right side of the index ({50,-51,1,1}) both equal 1.

Last one:
You are given the array {20,10,-80,10,10,15,35}
At index 0 the left side is {}
The right side is {10,-80,10,10,15,35}
They both are equal to 0 when added. (Empty arrays are equal to 0 in
this problem)
Index 0 is the place where the left side and right side are equal.
Note: Please remember that in most programming/scripting languages the
index of an array starts at 0.
Input:
An integer array of length 0 < arr < 1000. The numbers in the array can
be any integer positive or negative.
Output:
The lowest index N where the side to the left of N is equal to the side
to the right of N. If you do not find an index that fits these rules,
then you will return -1.
Note:
If you are given an array with multiple answers, return the lowest
correct index.

Third task

Your job is to group the words in anagrams.
What is an anagram ?
star and tsar are anagram of each other because you can rearrange the
letters for star to obtain tsar.
Example
A typical test could be :

// input
groupAnagrams(["tsar", "rat", "tar", "star", "tars", "cheese"]);
// output
[
["tsar", "star", "tars"],
["rat", "tar"],
["cheese"]
]

Fourth task

Unpack delicious sausages!

A food delivery truck carrying boxes of delicious sausages has arrived
and it's your job to unpack them and put them in the store's display
counter.
The truck is filled with boxes of goods. Among the goods, there are
various types of sausages. Straight sausages I, curvy sausages ), even
twirly sausages @ and many more. The safest way to tell any type of
sausage apart from other goods is by the packaging [], used exclusively
by sausages. Make sure to ignore other goods, those will be taken care
of by someone else. Once you have unpacked all the sausages, just lay
them out in the display counter (string) in the same order in which they
came in boxes with one space " " in-between every sausage. Oh, and watch
out for spoiled or damaged sausage packs, did I tell you about those?
The sausages are always packed in fours and each pack contains only one
sausage type, so whenever there is any irregularity, the sausages are
probably spoiled or damaged and the whole pack should be thrown out!
Now we're getting to the best part - your reward! Instead of money,
you'll be paid in something far better - sausages! Every fifth undamaged
processed pack of sausages doesn't go to the counter, instead it's yours
to keep.
Don't go spending it all at once!
If the truck arrives completely empty, only with empty boxes or only
with goods that are not sausages, the display counter will simply stay
empty "". Unlike truck and boxes that may be empty, every existing
product is a non-empty string.
Example:

Input (truck with 5 boxes containing 11 products):
[ [ "(-)", "[IIII]", "[))))]" ], [ "IuI", "[llll]" ], [ "[@@@@]", "UwU",
"[IlII]" ], [ "IuI", "[))))]", "x" ], [] ]
"Truck is an array, packages are arrays, packages of goods are strings"
Output (four sets of sausages):
"I I I I ) ) ) ) l l l l @ @ @ @"

Explanation:
The last box is empty and is therefore ignored
Packages with products that are not sausages are ignored - "(-)", "IuI",
"UwU", "IuI", "x"
One damaged package gets thrown out - "[IlII]"
Fifth undamaged package is used as your reward and is therefore excluded
from the output: "[))))]"
More examples of input and expected output can be seen in the example
test cases

Fifth task

Given an object of likely nested objects, where the final element is an
array containing positive integers, write a function that returns the
name of the root property that a particular integer lives in.
E.g
Heres what the object looks like:

object = {
"one": {
"nest1": {
"val1": [9, 34, 92, 100]
}
},
"2f7": {
"n1": [10, 92, 53, 71],
"n2": [82, 34, 6, 19]
}
}
getRootProperty(object, 9); //=> "one"

getRootProperty(object, 9) returns "one" because "one" is the root
property name where the value 9 is buried in (in an array), other root
properties may also have 9 buried in it but you should always return the
first
Another Example

object = {
"r1n": {
"mkg": {
"zma": [21, 45, 66, 111],
"mii": {
"ltf": [2, 5, 3, 9, 21]
},
"fv": [1, 3, 6, 9]
},
"rmk": {
"amr": [50, 50, 100, 150, 250]
}
},
"fik": {
"er": [592, 92, 32, 13]
"gp": [12, 34, 116, 29]
}
}
getRootProperty(object, 250); //=> "r1n"
getRootProperty(object, 116); //=> "fik"
getRootProperty(object, 111); //=> "r1n"
getRootProperty(object, 999); //=> null

return null if the value isn't found.

Sixth task

Given an array arr of strings, complete the function by calculating the
total perimeter of all the islands. Each piece of land will be marked
with 'X' while the water fields are represented as 'O'. Consider each
tile being a perfect 1 x 1 piece of land. Some examples for better
visualization:

['XOOXO',
'XOOXO',
'OOOXO',
'XXOXO',
'OXOOO']

which represents:
should return: "Total land perimeter: 24".
Following input:

['XOOO',
'XOXO',
'XOXO',
'OOXX',
'OOOO']

which represents:
should return: "Total land perimeter: 18"

@JestAK JestAK linked a pull request Nov 7, 2023 that will close this issue
@JestAK JestAK added the question Further information is requested label Nov 7, 2023
@JestAK JestAK self-assigned this Nov 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant