ughhhh
This assignment is an optional preview of the GCA test. You are required to take the GCA during Sprint 4, so we encourage you take your first attempt before Sprint 2 Module 2. We will record your highest attempt. You are not expected to know how to do everything on this test at this point in the curriculum. This is an optional preview of the test.
Follow this link to take the test, after reading the instructions below. You must take the test with your @lambdastudents.com (Links to an external site.) email address!
General Coding Assessment CodeSignal Test (Links to an external site.)
GCA FAQ and Info (Links to an external site.)
The GCA is a proctored assessment that provides a certified report you can use when applying for jobs. It is a standardized assessment that provides a high-level score for a candidate's programming skill. It maps programming skills to a score from 300 to 850, combining information about problem-solving and code-writing skills and speed and code quality. It is a measure meant to give recruiters, hiring managers, and educators – as well as the test-takers themselves – a quick view of the test-taker’s skills. Taking the assessment will be a data point that informs how career-ready you are and how you can continue to improve during Labs and Job Search. One key way that hiring managers are assessing new hires during remote work is with automated technical assessment testing, like GCA. This is why we will continue to evaluate how our students perform on these types of tests.
Be sure to take the test using the link above, and sign up with your @lambdastudents (Links to an external site.) e-mail. You must also select to share your results with Lambda so we can get your score back.
You will also need the following to take the test:
Grab your photo ID. Have a government-issued photo ID ready to show (like driver's license or passport).
Prepare your computer. Make sure you have the following: Chrome or Firefox, a reliable internet connection, webcam and microphone.
Time and space. Reserve 1 hour and 10 minutes to take the test somewhere quiet.
Share your webcam and desktop. When prompted, share your entire desktop screen as well as your webcam. Do not end video or close any windows or screensharing until the test is finished.
Again, you must take the test with your @lambdastudents (Links to an external site.) email address! This may require you to sign out of CodeSignal if you previously created an account and sign in with your @lambdastudents (Links to an external site.) email.
Each test draws from a pool of question types. There are four tasks for you to solve. Each test contains a very basic task, a simple data manipulation task, an implementation task (requires writing at least 25-40 lines of code, but the description clearly explains what needs to be done), and a problem-solving task (where you need to come up with the approach yourself).
The GCA test is limited to 1 hour and 10 minutes, starting from the time you begin the test.
You can retake the GCA after a 2 week 'cool off' period.
The test is proctored to guarantee the integrity of the assessment. Since GCA assessments are certified and shared across multiple companies, automated proctoring is required for the GCA. Without automated proctoring, for example, someone else can take the test for the given candidate and provide inaccurate test results. Note that Lambda School does not share your results with companies, but you may do so if you choose.
Virtual proctoring data is reviewed by CodeSignal only and is stored for no more than 15 days.
After the test, you can share your certification directly with employers or add it to your LinkedIn profile! Click here (Links to an external site.) to learn how. (Note that this is optional).
The GCA is a proctored assessment that provides a certified report you can use when applying for jobs. It is a standardized assessment that provides a high-level score for a candidate's programming skill. It maps programming skills to a score from 300 to 850, combining information about problem-solving and code-writing skills and speed and code quality. It is a measure meant to give recruiters, hiring managers, and educators – as well as the test-takers themselves – a quick view of the test-taker’s skills. Taking the assessment will be a data point that informs how career-ready you are and how you can continue to improve during Labs and Job Search. One key way that hiring managers are assessing new hires during remote work is with automated technical assessment testing, like GCA. This is why we will continue to evaluate how our students perform on these types of tests.
Be sure to take the test using the link above, and sign up with your @lambdastudents (Links to an external site.) e-mail. You must also select to share your results with Lambda so we can get your score back.
You will also need the following to take the test:
Grab your photo ID. Have a government-issued photo ID ready to show (like driver's license or passport).
Prepare your computer. Make sure you have the following: Chrome or Firefox, a reliable internet connection, webcam and microphone.
Time and space. Reserve 1 hour and 10 minutes to take the test somewhere quiet.
Share your webcam and desktop. When prompted, share your entire desktop screen as well as your webcam. Do not end video or close any windows or screensharing until the test is finished.
Again, you must take the test with your @lambdastudents (Links to an external site.) email address! This may require you to sign out of CodeSignal if you previously created an account and sign in with your @lambdastudents (Links to an external site.) email.
Each test draws from a pool of question types. There are four tasks for you to solve. Each test contains a very basic task, a simple data manipulation task, an implementation task (requires writing at least 25-40 lines of code, but the description clearly explains what needs to be done), and a problem-solving task (where you need to come up with the approach yourself).
The GCA test is limited to 1 hour and 10 minutes, starting from the time you begin the test.
You can retake the GCA after a 2 week 'cool off' period.
The test is proctored to guarantee the integrity of the assessment. Since GCA assessments are certified and shared across multiple companies, automated proctoring is required for the GCA. Without automated proctoring, for example, someone else can take the test for the given candidate and provide inaccurate test results. Note that Lambda School does not share your results with companies, but you may do so if you choose.
Virtual proctoring data is reviewed by CodeSignal only and is stored for no more than 15 days.
After the test, you can share your certification directly with employers or add it to your LinkedIn profile! Click here (Links to an external site.) to learn how. (Note that this is optional).
The test is four problems, which are CodeSignal/HackerRank/Leetcode-style programming challenges. Problem 1 is generally the easiest, progressing to the hardest with problem 4.
The result score is between 300 and 850 (best). 650 is about the low end for Junior Dev.
This test is about your problem-solving skill, not about your Googling skill. You are expected to ponder the problems and come up with your own solution. Pasting solutions in from elsewhere and trying to make them work is prohibited. Even googling for another solution is prohibited. (You can, however, search for syntax help.)
For this reason, you should practice this skill throughout CS. CS is all about using UPER to solve problems you've never seen before without Googling, and the GCA measures this skill.
Since GCA is designed to measure skills that are important for almost all software developers, CodeSignal has aimed to find the common denominator between three different sources of data.
What are the most common topics taught in different CS programs at 4-year Universities in the US?
What are the most common topics covered during technical interviews at successful US-based companies?
What are the most common questions asked on StackOverflow that are about general programming and not specialized domain knowledge?
CodeSignal has used MIT OCW, EdX, Coursera, and Udacity course catalogs as a source for #1. They've used the book Cracking the Coding Interview, CodeSignal Interview Practice Mode, Leetcode, CareerCup, and Glassdoor Interview Questions sections to identify #2. And StackOverflow public API for #3.
Do the coursework in Lambda CS.
Try to generally restrict your searching to syntax unless otherwise directed
Don't search for problem solutions for the sprint challenges; syntax only
Same for the GCA, proctor-enforced
Additional, optional resources:
Go to the CodeSignal Arcade and solve questions in The Core without looking up the answer. If you look up the answer, it doesn't count.
Solve the first 50 problems
Except spend your earned coins to skip Corner of 0s and 1s
Some of these are challenging--feel free to buy your way ahead if you need to come back to a hard problem later
Keep solving more for more practice.
Once you get a few hundred coins, unlock the interview practice and other things in the main menu.
Head over to Leetcode and tackle the easy and medium leetcode algorithms problems, or problems on your site of choice.
Take the GCA Practice Test (24-hour cooldown).
If you get 100% of the tests passing on a submission for a set of problems, you'll get a base score, listed in the table below. This base score is modified up or down based on a variety of factors.
The score is modified ±12 points based on three factors:
Number of attempts
Your score will be modded if you make more or fewer than the average number of submissions for a particular problem.
Running the tests doesn't count as a submission. You must click the Submit
button for it to count.
Time taken
Your score will be modded if you take longer or shorter than the average amount of time to solve a problem.
Code quality
Your code quality will be automatically judged based on a variety of factors, e.g.: consistent spacing and indentation.
Again, the most the base score will change as determined by these factors is ±12 points.
Strategy recommendation: don't worry about the modifiers. Just concentrate on UPER and solving the problem.
If you submit and pass fewer than 100% of the tests, you'll receive partial credit for the submission depending on how many tests you did pass. In other words, you won't achieve the base score for that problem, but will get part way toward the base score.
If you get 100% of tests passing for the listed questions in the left column, your base score is in the center. On the right is the modifier range, ±12 points from the base score.
For example, if you get questions 1, 2, and 3 100% correct, but you make a lot of incorrect submissions and take longer than average, you'll to score closer to 751. If you get them correct in the first submission in record time, you'll score closer to 775.
Strategy recommendation: Choose the problem that looks the easiest to tackle first. This is likely question 1, but read them all to find out.
In addition to the numeric score, additional ratings are presented. Lambda does not use these ratings, but they are included for your information.
Speed is your rating compared to the average speed to solve the problems.
The other two ratings are determined by which problems are solved, as shown below:
NOTE: these aren't definitive or complete lists! They don't say exactly what will be on the test, and the test questions might require more or less knowledge than listed. The information below is included to give you an idea of the relative difficulty of each question.
Working with numbers.
Basic operations with numbers.
Basic string manipulation.
Splitting a string into substrings.
Modifying the elements of a string.
Basic array manipulation.
Iterating over an array.
Tasks that require a combination of 2 to 3 basic concepts. For example:
Iterating over an array and taking into account some condition.
Splitting a string by some condition.
Should usually be solvable using one loop.
The task description should clearly state the implementation steps.
Working with numbers.
Basic operations with numbers.
Splitting numbers into digits.
Basic string manipulation.
Splitting a string into substrings.
Comparing strings.
Modifying the elements of a string. – Concatenating strings.
Reversing a string.
Basic array manipulation.
Iterating over an array.
Modifying the elements of an array.
Reversing an array.
Tasks that require a combination of 3 to 5 basic concepts. For example:
Splitting a string into substrings, modifying each substring and comparing with other strings.
Iterating over an array to produce two new arrays given some conditions, modifying the second array and appending it to the beginning of the first array.
Should usually be solvable using one to two nested loops.
The task description should clearly state the implementation steps.
Includes everything from the previous task.
Splitting a task into smaller subtasks/functions.
Manipulating two-dimensional arrays.
Iterating over the elements in a particular order.
Modifying values.
Swapping rows/columns.
Using hashmaps.
Using built in hashmaps to store strings or integers as keys.
Implementing a specific comparator for strings.
Implementing a specific merge function for arrays.
Other implementation challenges that clearly explain what needs to be done and require translating the instructions into code.
Includes everything from previous tasks.
Working with trees.
Storing and traversing trees.
Transforming trees.
Understanding hashmaps.
Solving tasks that require understanding the implementation of hashmaps.
Fundamentals of discrete mathematics.
Brute-force search.
Checking all possible solutions to find the optimal solution.
Tasks that require noticing an application of a certain algorithm, data-structure or technique.
Optimizing some queries with the help of data structures like hashmaps or sets.
Algorithms on trees like finding the longest path of a tree.
When you start the General Coding Assessment, you are required to setup proctoring, which allows our team to verify the work was your own. To learn more about proctoring and the requirements, visit the What is Proctoring article here. We also have a video resource which walks you through the Certify proctoring steps here.
Below is a screenshot of the general rules you will see once you have completed the Proctoring Setup. Please read through the rules below and note you will need to abide by each one in order for your test results to be certified.
After you have successfully setup proctoring, you will see a second set of rules before you are able to begin the test. You must check the box next to each statement to acknowledge your consent. You are also able to select your preferred coding language here. Note you can update the coding language directly in the IDE once in the exam.
Through not rules, candidates frequently reach out regarding questions about the General Coding Assessment setup and structure. Below we will address the setup. You can learn more about the structure of the General Coding Assessment by viewing our separate help article.
Please note the test setup below.
You will have 70 minutes to complete the exam in one sitting. You are able to move between tasks, though you must submit your work before leaving a task in order for the code to save. You are allowed to submit solutions as much as needed. You will only be graded on your final submitted solution at the time you click Finish the Test. If you run out of time, your last submitted solution will be the one graded.
Additional clarifications to the rules:
Using scratch paper is allowed, but make it obvious (and maybe even say) that you're scribbling.
Using an off-screen whiteboard is allowed, but make it obvious that's what you're doing.
Recommend against referring to written notes since the proctor won't know what you're looking at.
Scan or take photos of your relevant notes so you can view them on-screen.
Prohibited: PythonTutor or any other external IDE, editor, debugger, or environment.
Exception: You may open a simple Python REPL in a terminal to quickly test commands or look up syntax.
SIGN IN WITH YOUR LAMBDASTUDENTS EMAIL! If you're not sure, go to your CodeSignal profile and make sure it's set as your primary email.
IF YOU DON'T, YOUR ATTEMPT WON'T COUNT!
There is a 2-week cooldown (measured down to the minute of your previous submission).
If this link fails, DM @Beej
on Slack to get it updated.
What to Expect on the GCA including a link to the practice test
Correct Answers
Base Score
Modifier Range
1 . . .
662
650-674
. 2 . .
700
688-712
1 2 . .
712
700-724
. . 3 .
731
719-743
1 . 3 .
743
731-755
. 2 3 .
750
738-762
1 2 3 .
763
751-775
. . . 4
780
768-792
1 . . 4
792
780-804
. 2 . 4
799
787-811
. . 3 4
805
793-817
1 2 . 4
812
800-824
1 . 3 4
818
806-830
. 2 3 4
825
813-837
1 2 3 4
837
825-849
Solved Tasks
Implementation
Problem-Solving
1 . . .
Low
Low
. 2 . .
Fair
Fair
1 2 . .
Fair
Fair
1 2 3 .
Good
Average
1 2 . 4
Good
Good
1 2 3 4
Excellent
Excellent
"""
GCA Practice
"""
"""
Mutate the array
----------------
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]
"""
a = [4, 0, 1, -2, 3]
n = 5
# passing
def mutateTheArray(n, a):
if len(a) < 2:
return a
result = []
for i in range(len(a)):
# if i = 0 then i - 1 does not exist so [i-1] becomes 0 and we can
# just leave it off
if i == 0:
result.append(a[i] + a[i + 1])
# if i is pointing to the last element then [i + 1] does not exist so
# it becomes 0 and we can just leave that off
elif i == len(a) - 1:
result.append((a[i - 1] + a[i]))
# for all other cases just do the normal equation
else:
result.append(a[i - 1] + a[i] + a[i + 1])
return result
# print(mutateTheArray(n, a))
"""
Alternating sort
You are given an array of integers a. A new array b is generated by rearranging the elements of a in the following way:
b[0] is equal to a[0];
b[1] is equal to the last element of a;
b[2] is equal to a[1];
b[3] is equal to the second-last element of a;
b[4] is equal to a[2];
b[5] is equal to the third-last element of a;
and so on.
Here is how this process works:
Your task is to determine whether the new array b is sorted in strictly ascending order or not.
Example
For a = [1, 3, 5, 6, 4, 2], the output should be alternatingSort(a) = true.
The new array b will look like [1, 2, 3, 4, 5, 6], which is in strictly ascending order, so the answer is true.
For a = [1, 4, 5, 6, 3], the output should be alternatingSort(a) = false.
The new array b will look like [1, 3, 4, 6, 5], which is not in strictly ascending order, so the answer is false.
"""
a = [1, 3, 5, 6, 4, 2]
# a = [1, 4, 5, 6, 3]
# a = [-92, -23, 0, 45, 89, 96, 99, 95, 89, 41, -17, -48]
a = [-91, -84, -67, -44, 9, 70, 88, 37, -11, -67, -72, -87]
a = [-99, -29, -7, 17, 28, 71, 98, 86, 42, 22, 0, -29, -86]
# passing
from collections import deque
def alternatingSort(a):
a = deque(a)
if len(a) < 2:
return True
if a == []:
return False
# array to hold result
b = []
# sorted a without duplicates to check against result array to see if
# result is sorted or not
match = sorted(set(a))
# while a still has numbers to sort into result array
while len(a) > 0:
# if there is an even number of items left
if len(b) % 2 == 0:
# remove first element and add to result
b.append(a.popleft())
# else if there is an odd number of items left
else:
# remove last element and add it to the result
b.append(a.pop())
# if result == the original array sorted then return True else return False
if b == match:
return True
return False
# print(alternatingSort(a))
"""
Tiny pairs
----------
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.
"""
a = [16, 1, 4, 2, 14]
b = [7, 11, 2, 0, 15]
k = 743
# passing
def countTinyPairs(a, b, k):
# variable to hold the number of tiny pairs we've encountered
pairs = 0
# index of the last number in the array
b_index = len(b) - 1
# iterate the array
for i in range(len(a)):
# if the integer value of the string concatenation of the number at
# the first index and the number at the last index is less than K
# we found a tiny pair and can increment the pairs value + 1
if int(str(a[i]) + str(b[b_index])) < k:
pairs += 1
# decrement the last index to check the next to last number
b_index -= 1
# return the number of pairs found
return pairs
# print('pairs', countTinyPairs(a, b, k))
"""
Merging Strings
---------------
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 lexicographical 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.
Example
For s1 = "dce" and s2 = "cccbd", the output should be
mergeStrings(s1, s2) = "dcecccbd".
All symbols from s1 goes first, because all of them have only 1 occurrence in s1 and c has 3 occurrences in s2.
For s1 = "super" and s2 = "tower", the output should be
mergeStrings(s1, s2) = "stouperwer".
Because in both strings all symbols occur only 1 time, strings are merged as usual. You can find explanation for this example on the image in the description.
Input/Output
[execution time limit] 4 seconds (py3)
[input] string s1
A string consisting only of lowercase English letters.
Guaranteed constraints:
1 ≤ s1.length ≤ 104.
[input] string s2
A string consisting only of lowercase English letters.
Guaranteed constraints:
1 ≤ s2.length ≤ 104.
[output] string
The string that results by merging s1 and s2 using your special merge function.
"""
s1 = "super"
s2 = "tower"
s1 = "dce"
s2 = "cccbd"
s1 = "kkihj"
s2 = "jbsmfoftph"
expected = "jbsmfoftphkkihj"
s1 = "ougtaleegvrabhugzyx"
s2 = "wvieaqgaegbxg"
output = "owvieaqugaegbxggtaleegvrabhugzyx"
expected = "owvieaqugtaleegvrabhugzyxgaegbxg"
# fully passing
def mergeStrings(s1, s2):
# variable to hold the result
result = ''
# variable to hold the current index of the longer of the two strings
s2_index = 0
# variable to hold the current index of the shorter of the two strings
s1_index = 0
# variables for the s2 string and the s1 string
# s2 = ''
# s1 = ''
# logic to find the s2 and s1 strings from the given strings
# if len(s1) > len(s2):
# s2 = s1
# s1 = s2
# else:
# s2 = s2
# s1 = s1
# hash maps for each string to hold the number of occurrences of each
# letter
s1_map = {}
s2_map = {}
# logic creating the hash maps
for letter in s1:
if letter not in s1_map:
s1_map[letter] = 0
s1_map[letter] += 1
for letter in s2:
if letter not in s2_map:
s2_map[letter] = 0
s2_map[letter] += 1
# print('s1 map:', s1_map)
# print('s2 map:', s2_map)
# while the s1 string current index is less than the s1 string length
# and the s2 string index is shorter than the s2 string length
# (keeps us from index out of bounds error)
while s1_index < len(s1) and s2_index < len(s2):
# print('s1', s1)
# print('s2', s2)
# print('result:', result)
# print('compare', 's1', s1[s1_index], 's2', s2[s2_index])
# my print statements for debugging
# print('i', i)
# print('s2_index', s2_index)
# print(chr(max(ord(s1[i]), ord(s2[s2_index]))))
# if the letter count for the s1 string letter at current s1
# string index is less than the letter count for the s2 string
# letter at current s2 string index
if s1_map[s1[s1_index]] < s2_map[s2[s2_index]]:
# print('s1 i', s1[i])
# print('count', s1_map[s1[i]])
# print('s2 i', s2[s2_index])
# print('count', s2_map[s2[s2_index]])
# add the letter from the s1 string at the current index to
# the result
result += s1[s1_index]
# increment the s1 string current index
s1_index += 1
# if the letter count for the s1 string letter at the current
# index is greater than the letter count for the s2 string letter
# at current s2 string index
elif s1_map[s1[s1_index]] > s2_map[s2[s2_index]]:
# add the letter from the s2 string at the current index to the
# result
result += s2[s2_index]
# increment the s2 string current index
s2_index += 1
# if both letter have the same number of occurrences in their
# respective strings
else:
if ord(s1[s1_index]) == ord(s2[s2_index]):
result += s1[s1_index]
s1_index += 1
else:
# add the letter that comes first sequentially
result += chr(min(ord(s1[s1_index]), ord(s2[s2_index])))
# if the s2 string letter was added
if ord(s1[s1_index]) > ord(s2[s2_index]):
# increment the s2 string current index
s2_index += 1
# else if the s1 string letter was added
else:
# increment the s1 string current index
s1_index += 1
# if the s1 string index is at the end of the string
# we have added all the letters in that string and can just add the rest
# of the letters in the s2 string to the result in the order they are in
if s1_index == len(s1):
# add rest of the s2 string from the current index to the end of
# the string
result += s2[s2_index:]
# if the s2 string index is at the end of the string
# we have added all the letters in that string and can just add the rest
# of the letters in the s1 string to the result in the order they are in
if s2_index >= len(s2):
# add rest of the s1 string from the current index to the end of
# the string
result += s1[s1_index:]
# return the result
return result
# print('merge strings', mergeStrings(s1, s2))
"""
Concatenations Sum
------------------
Given an array of positive integers a, your task is to calculate the sum of every possible a[i] ∘ a[j], where a[i] ∘ a[j] is the concatenation of the string representations of a[i] and a[j] respectively.
Example
For a = [10, 2], the output should be concatenationsSum(a) = 1344.
a[0] ∘ a[0] = 10 ∘ 10 = 1010,
a[0] ∘ a[1] = 10 ∘ 2 = 102,
a[1] ∘ a[0] = 2 ∘ 10 = 210,
a[1] ∘ a[1] = 2 ∘ 2 = 22.
So the sum is equal to 1010 + 102 + 210 + 22 = 1344.
For a = [8], the output should be concatenationsSum(a) = 88.
There is only one number in a, and a[0] ∘ a[0] = 8 ∘ 8 = 88, so the answer is 88.
For a = [1, 2, 3], the output should be concatenationsSum(a) = 198.
a[0] ∘ a[0] = 1 ∘ 1 = 11,
a[0] ∘ a[1] = 1 ∘ 2 = 12,
a[0] ∘ a[2] = 1 ∘ 3 = 13,
a[1] ∘ a[0] = 2 ∘ 1 = 21,
a[1] ∘ a[1] = 2 ∘ 2 = 22,
a[1] ∘ a[2] = 2 ∘ 3 = 23,
a[2] ∘ a[0] = 3 ∘ 1 = 31,
a[2] ∘ a[1] = 3 ∘ 2 = 32,
a[2] ∘ a[2] = 3 ∘ 3 = 33.
The total result is 11 + 12 + 13 + 21 + 22 + 23 + 31 + 32 + 33 = 198.
"""
a = [8]
a = [1, 2, 3]
# a = [0, 0]
# needs to be optimized to pass all tests currently 250/300
# def concatenationsSum(a):
# # variable for the result of the integer addition of concatenated strings
# result = 0
# # variables for the current index and the index of the number being added
# front_index = 0
# back_index = 0
# # iterate the array
# while front_index < len(a):
# # if the number being added is the last one
# if back_index == len(a) - 1:
# # do the concatenation
# result += int(str(a[front_index]) + str(a[back_index]))
# # increment the current index
# front_index += 1
# # reset the sum index to 0
# back_index = 0
# # otherwise just do the concatenation and increase the index of
# # number being added
# else:
# result += int(str(a[front_index]) + str(a[back_index]))
# back_index += 1
#
# # return the result
# return result
def concatenationsSum(a):
# UPER
# Understand:
# we're going to need to return an int after concatenating strings --> int(str() ...)
# Need a sum: start it at 0
concatentations_sum = 0
# "every possible a[i] and a[j]" --> 2 indices, or 2 nested for loops
# iterate over the array
# for each index front_index:
for front_index in range(len(a)):
# front_index is the index of the first element (front of the concatenation)
# visit every other index to hit the possible combinations
# for each front_index, we want to visit _every_ j / back_index (even if j == i, or j < i)
for back_index in range(len(a)):
# back_index is the index of the second / back of the concatenation
# how do we concatenate?
# we need to convert to strings using str
# # str(a[front_index]), str(a[back_index])
# front_string = str(a[front_index])
# back_string = str(a[back_index])
# concatenate the strings using +
concatenated_string = str(front_string) + str(back_string)
# convert back to int using int()
concatenated_int = int(concatenated_string)
# add the result of ^ to sum
concatentations_sum += concatenated_int
return concatentations_sum
#
test_cases = [
([10, 2], 1344),
([8], 88),
([1, 2, 3], 198),
]
for input, output in test_cases:
print(f"For input: {input} expecting {output}")
actual_output = concatenationsSum(input)
print(f"Actual output: {actual_output}")
print("----")
# print(concatenationsSum(a))
"""
HashMap
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.
Example
For queryType = ["insert", "insert", "addToValue", "addToKey", "get"] and query = [[1, 2], [2, 3], [2], [1], [3]], the output should be hashMap(queryType, query) = 5.
The hashmap looks like this after each query:
1 query: {1: 2}
2 query: {1: 2, 2: 3}
3 query: {1: 4, 2: 5}
4 query: {2: 4, 3: 5}
5 query: answer is 5
The result of the last get query for 3 is 5 in the resulting hashmap.
For queryType = ["insert", "addToValue", "get", "insert", "addToKey", "addToValue", "get"] and query = [[1, 2], [2], [1], [2, 3], [1], [-1], [3]], the output should be hashMap(queryType, query) = 6.
The hashmap looks like this after each query:
1 query: {1: 2}
2 query: {1: 4}
3 query: answer is 4
4 query: {1: 4, 2: 3}
5 query: {2: 4, 3: 3}
6 query: {2: 3, 3: 2}
7 query: answer is 2
The sum of the results for all the get queries is equal to 4 + 2 = 6.
Input/Output
[execution time limit] 4 seconds (py3)
[input] array.string queryType
Array of query types. It is guaranteed that each queryType[i] is either "addToKey", "addToValue", "get", or "insert".
Guaranteed constraints:
1 ≤ queryType.length ≤ 105.
[input] array.array.integer query
Array of queries, where each query is represented either by two numbers for insert query or by one number for other queries. It is guaranteed that during all queries all keys and values are in the range [-109, 109].
Guaranteed constraints:
query.length = queryType.length,
1 ≤ query[i].length ≤ 2.
[output] integer64
The sum of the results for all get queries.
"""
queryType = ["insert", "addToValue", "get", "insert", "addToKey", "addToValue",
"get"]
query = [[1, 2], [2], [1], [2, 3], [1], [-1], [3]]
queryType = ["insert", "insert", "addToValue", "addToKey", "get"]
query = [[1, 2], [2, 3], [2], [1], [3]]
# this is not currently passing all tests but most
def hashMap(queryType, query):
# we want to build on top of pythons built in {}
# we need our hashmap {}
hash_map = {}
# make helper functions for the different queries (get, insert, addToValue, addToKey)
sum_of_gets = 0
# iterate through queryType and query and call the corresponding helper functions
for i in range(len(queryType)):
method_name = queryType[i]
parameters = query[i]
# print(method_name, parameters)
if method_name == "insert":
hash_map = handle_insert(hash_map, parameters)
elif method_name == "get":
value = handle_get(hash_map, parameters)
sum_of_gets += value
elif method_name == "addToKey":
hash_map = handle_add_to_key(hash_map, parameters)
elif method_name == "addToValue":
hash_map = handle_add_to_value(hash_map, parameters)
return sum_of_gets
# keep track of the sum of get queries
def handle_insert(hash_map, parameters):
key = parameters[0]
value = parameters[1]
hash_map[key] = value
return hash_map
def handle_get(hash_map, parameters):
key = parameters[0]
return hash_map[key]
def handle_add_to_key(hash_map, parameters):
offset = parameters[0]
# add `offset` to every key in hash_map
# safer to create a new dictionary
new_hash_map = {}
# iterate through the keys and values in the old dictionary
for key, value in hash_map.items():
# add `offset` to the key and put it into the new dictionary
new_key = key + offset
new_hash_map[new_key] = value
return new_hash_map
def handle_add_to_value(hash_map, parameters):
offset = parameters[0]
# we want to add offset to every value in hash_map
for key, value in hash_map.items():
new_value = value + offset
hash_map[key] = new_value
return hash_map
test_cases = [
{"queryType": ["insert", "insert", "addToValue", "addToKey", "get"],
"query": [[1, 2], [2, 3], [2], [1], [3]],
"expected_output": 5},
{"queryType": ["insert", "addToValue", "get", "insert", "addToKey",
"addToValue", "get"],
"query": [[1, 2], [2], [1], [2, 3], [1], [-1], [3]],
"expected_output": 6},
{"queryType": ["addToKey", "addToKey", "insert", "addToValue", "addToValue",
"get", "addToKey", "insert",
"addToKey", "addToValue"],
"query": [[-3], [-1], [0, -3], [3], [-1], [0], [-1], [-4, -5], [-1], [-4]],
"expected_output": -1},
{"queryType": ["insert", "insert", "addToKey", "addToKey", "addToKey",
"insert", "addToValue", "addToKey",
"addToKey", "get"],
"query": [[-5, -2], [2, 4], [-1], [-3], [1], [3, -2], [-4], [-2], [2],
[-8]],
"expected_output": -6},
{"queryType": ["insert", "get", "insert", "addToValue", "addToValue",
"addToValue", "insert", "addToKey", "get",
"insert"],
"query": [[2, 1], [2], [1, 3], [-1], [0], [3], [4, -5], [3], [4], [1, 1]],
"expected_output": 6},
{"queryType": ["addToValue", "addToValue", "insert", "get", "addToKey",
"insert", "insert", "insert", "addToValue",
"addToKey"],
"query": [[-5], [3], [3, -3], [3], [0], [-4, 2], [0, -3], [-2, 4], [2],
[2]],
"expected_output": -3},
{"queryType": ["addToKey", "addToKey", "insert", "addToKey", "addToValue",
"addToKey", "addToValue", "insert", "get",
"insert"],
"query": [[-1], [-3], [4, 3], [2], [2], [-2], [0], [-5, 3], [-5], [3, -4]],
"expected_output": 3},
{"queryType": ["insert", "insert", "insert", "get", "insert", "insert",
"insert", "addToKey", "insert", "insert"],
"query": [[3, -4], [-4, 3], [4, -3], [4], [-5, 0], [-2, -5], [2, 2], [1],
[-5, -2], [1, 3]],
"expected_output": -3},
{"queryType": ["addToValue", "addToKey", "addToKey", "insert", "addToValue",
"addToValue", "insert", "get", "get", "insert"],
"query": [[-2], [-3], [0], [-3, 1], [-2], [-4], [2, -4], [2], [2],
[3, -1]],
"expected_output": -8}
]
for test_case in test_cases:
query_type = test_case['queryType']
query = test_case['query']
print(
f"Input:\nqueryType: {query_type}\nquery: {query}\nexpected_output: {test_case['expected_output']}")
actual_output = hashMap(query_type, query)
print(f"Actual output: {actual_output}")
print("-----")
"""
Mean Groups
-----------
You are given an array of arrays a. Your task is to group the arrays a[i] by their mean values, so that arrays with equal mean values are in the same group, and arrays with different mean values are in different groups.
Each group should contain a set of indices (i, j, etc), such that the corresponding arrays (a[i], a[j], etc) all have the same mean. Return the set of groups as an array of arrays, where the indices within each group are sorted in ascending order, and the groups are sorted in ascending order of their minimum element.
Example
For
a = [[3, 3, 4, 2],
[4, 4],
[4, 0, 3, 3],
[2, 3],
[3, 3, 3]]
the output should be
meanGroups(a) = [[0, 4],
[1],
[2, 3]]
mean(a[0]) = (3 + 3 + 4 + 2) / 4 = 3;
mean(a[1]) = (4 + 4) / 2 = 4;
mean(a[2]) = (4 + 0 + 3 + 3) / 4 = 2.5;
mean(a[3]) = (2 + 3) / 2 = 2.5;
mean(a[4]) = (3 + 3 + 3) / 3 = 3.
There are three groups of means: those with mean 2.5, 3, and 4. And they form the following groups:
Arrays with indices 0 and 4 form a group with mean 3;
Array with index 1 forms a group with mean 4;
Arrays with indices 2 and 3 form a group with mean 2.5.
Note that neither
meanGroups(a) = [[0, 4],
[2, 3],
[1]]
nor
meanGroups(a) = [[0, 4],
[1],
[3, 2]]
will be considered as a correct answer:
In the first case, the minimal element in the array at index 2 is 1, and it is less then the minimal element in the array at index 1, which is 2.
In the second case, the array at index 2 is not sorted in ascending order.
For
a = [[-5, 2, 3],
[0, 0],
[0],
[-100, 100]]
the output should be
meanGroups(a) = [[0, 1, 2, 3]]
The mean values of all of the arrays are 0, so all of them are in the same group.
"""
a = [[0, 4],
[2, 3],
[1]]
a = [[3, 3, 4, 2],
[4, 4],
[4, 0, 3, 3],
[2, 3],
[3, 3, 3]]
# passing all tests
def meanGroups(a):
# create dict to hold mean and arrays w that mean
means = {}
# iterate outer array
for i in range(len(a)):
# get the mean for each inner array
sum = 0
for num in a[i]:
sum += num
mean = sum / len(a[i])
# hold array and mean in dictionary
if mean not in means:
means[mean] = []
means[mean].append(i)
# return the set of each unique mean with first occurring inner array listed first
result = []
for mean in means:
result.append(means[mean])
return result
# print(meanGroups(a))
def add(param1, param2):
return param1 + param2
#------------------------------------------------------------------------------------------------#
#------------------------------------------------------------------------------------------------#
def centuryFromYear(year):
return ((year - 1) // 100) + 1
#------------------------------------------------------------------------------------------------#
def checkPalindrome(inputString):
return inputString == inputString[::-1]
#------------------------------------------------------------------------------------------------#
def adjacentElementsProduct(inputArray):
max = inputArray[0] * inputArray[1]
for i in range(len(inputArray) - 1):
if inputArray[i] * inputArray[i + 1] > max:
max = inputArray[i] * inputArray[i + 1]
return max
#------------------------------------------------------------------------------------------------#
def shapeArea(n):
sum = n * 2 - 1
for i in range(1, (n * 2) - 1, 2):
sum += i * 2
return sum
#------------------------------------------------------------------------------------------------#
def makeArrayConsecutive2(statues):
return max(statues) - min(statues) - len(statues) + 1
#------------------------------------------------------------------------------------------------#
def almostIncreasingSequence(sequence):
i = 0
while i < len(sequence) - 1:
if not sequence[i] < sequence[i + 1]:
if increasingSequence(
sequence[:i] + sequence[i + 1 :]
) or increasingSequence(sequence[: i + 1] + sequence[i + 2 :]):
return True
else:
return False
i += 1
return True
#------------------------------------------------------------------------------------------------#
def increasingSequence(sequence):
for i in range(len(sequence) - 1):
if not sequence[i] < sequence[i + 1]:
return False
return True
#------------------------------------------------------------------------------------------------#
def matrixElementsSum(matrix):
if len(matrix) > 1:
for row in range(1, len(matrix)):
for room in range(len(matrix[row])):
if matrix[row - 1][room] == 0:
matrix[row][room] = 0
sum = 0
for row in matrix:
for room in row:
sum += room
return sum
#------------------------------------------------------------------------------------------------#
def allLongestStrings(inputArray):
length = max([len(word) for word in inputArray])
result = [word for word in inputArray if len(word) == length]
return result
#------------------------------------------------------------------------------------------------#
def commonCharacterCount(s1, s2):
count = 0
word2 = list(s2)
for letter in s1:
if letter in word2:
word2.remove(letter)
count += 1
return count
#------------------------------------------------------------------------------------------------#
def isLucky(n):
string = str(n)
top = [int(x) for x in string[: len(string) // 2]]
bottom = [int(x) for x in string[len(string) // 2 :]]
return sum(top) == sum(bottom)
#------------------------------------------------------------------------------------------------#
def sortByHeight(a):
treePositions = [x for x in range(len(a)) if a[x] == -1]
people = sorted([x for x in a if x != -1])
for tree in treePositions:
people.insert(tree, -1)
return people
import re
#------------------------------------------------------------------------------------------------#
def reverseParentheses(s):
while "(" in s:
match = re.search("\([^()]*\)", s)
match_string = match.group(0)[1 : len(match.group(0)) - 1]
reversed_match_string = match_string[::-1]
s = s[: match.start()] + reversed_match_string + s[match.end() :]
return s
#------------------------------------------------------------------------------------------------#
def alternatingSums(a):
team1 = sum(a[0::2])
team2 = sum(a[1::2])
return [team1, team2]
#------------------------------------------------------------------------------------------------#
def addBorder(picture):
picture = ["*" + string + "*" for string in picture]
picture = [("*" * len(picture[0]))] + picture + [("*" * len(picture[0]))]
return picture
#------------------------------------------------------------------------------------------------#
def areSimilar(a, b):
diff = [i for i in range(len(a)) if a[i] != b[i]]
if len(diff) == 2:
b[diff[0]], b[diff[1]] = b[diff[1]], b[diff[0]]
return a == b
#------------------------------------------------------------------------------------------------#
def arrayChange(inputArray):
count = 0
for i in range(1, len(inputArray)):
if inputArray[i - 1] >= inputArray[i]:
difference = inputArray[i - 1] - inputArray[i]
inputArray[i] += difference + 1
count += difference + 1
return count
#------------------------------------------------------------------------------------------------#
def palindromeRearranging(inputString):
inputList = sorted(inputString)
foundMiddle = False
while len(inputList) > 1:
if inputList[0] == inputList[1]:
del inputList[1]
elif not foundMiddle:
foundMiddle = True
else:
return False
del inputList[0]
return len(inputList) == 0 or not foundMiddle
#------------------------------------------------------------------------------------------------#
def areEquallyStrong(yourLeft, yourRight, friendsLeft, friendsRight):
sameHands = yourLeft == friendsLeft and yourRight == friendsRight
differentHands = yourLeft == friendsRight and yourRight == friendsLeft
return sameHands or differentHands
#------------------------------------------------------------------------------------------------#
def arrayMaximalAdjacentDifference(inputArray):
diffs = []
for i in range(len(inputArray) - 1):
diffs.append(abs(inputArray[i] - inputArray[i + 1]))
return max(diffs)
#------------------------------------------------------------------------------------------------#
def isIPv4Address(inputString):
strings = [string for string in inputString.split(".")]
for string in strings:
if not string.isdecimal():
return False
nums = [int(num) for num in strings]
return max(nums) <= 255 and min(nums) >= 0 and len(nums) == 4
#------------------------------------------------------------------------------------------------#
def avoidObstacles(inputArray):
for length in range(2, max(inputArray) + 2):
done = True
jump = length
while jump < (max(inputArray) + length):
if jump in inputArray:
done = False
break
jump += length
if done:
return length
#------------------------------------------------------------------------------------------------#
def boxBlur(image):
outImage = []
for row in range(1, len(image) - 1):
line = []
for pixel in range(1, len(image[row]) - 1):
total = (
image[row - 1][pixel - 1]
+ image[row - 1][pixel]
+ image[row - 1][pixel + 1]
+ image[row][pixel - 1]
+ image[row][pixel]
+ image[row][pixel + 1]
+ image[row + 1][pixel - 1]
+ image[row + 1][pixel]
+ image[row + 1][pixel + 1]
)
line.append(total // 9)
outImage.append(line)
return outImage
#------------------------------------------------------------------------------------------------#
def minesweeper(matrix):
TOP = 0
BOTTOM = len(matrix) - 1
LEFT = 0
RIGHT = len(matrix[0]) - 1
outMatrix = []
for row in range(len(matrix)):
outRow = []
for cell in range(len(matrix[row])):
outRow.append(0)
if row != TOP:
outRow[cell] += matrix[row - 1][cell]
if row != BOTTOM:
outRow[cell] += matrix[row + 1][cell]
if cell != LEFT:
outRow[cell] += matrix[row][cell - 1]
if cell != RIGHT:
outRow[cell] += matrix[row][cell + 1]
if row != TOP and cell != LEFT:
outRow[cell] += matrix[row - 1][cell - 1]
if row != TOP and cell != RIGHT:
outRow[cell] += matrix[row - 1][cell + 1]
if row != BOTTOM and cell != LEFT:
outRow[cell] += matrix[row + 1][cell - 1]
if row != BOTTOM and cell != RIGHT:
outRow[cell] += matrix[row + 1][cell + 1]
outMatrix.append(outRow)
return outMatrix
#------------------------------------------------------------------------------------------------#
def arrayReplace(inputArray, elemToReplace, substitutionElem):
return [x if x != elemToReplace else substitutionElem for x in inputArray]
#------------------------------------------------------------------------------------------------#
def evenDigitsOnly(n):
return all(
(True if digit in ("0", "2", "4", "6", "8") else False for digit in str(n))
)
#------------------------------------------------------------------------------------------------#
def variableName(name):
return name.replace("_", "").isalnum() and not name[0].isdigit()
#------------------------------------------------------------------------------------------------#
def alphabeticShift(inputString):
return "".join([chr(ord(x) + 1) if x != "z" else "a" for x in inputString])
#------------------------------------------------------------------------------------------------#
def chessBoardCellColor(cell1, cell2):
color1 = ((ord(cell1[0]) - ord("A")) + ord(cell1[1]) - ord("1")) % 2 == 0
color2 = ((ord(cell2[0]) - ord("A")) + ord(cell2[1]) - ord("1")) % 2 == 0
return color1 == color2
#------------------------------------------------------------------------------------------------#
def circleOfNumbers(n, firstNumber):
return (firstNumber + (n / 2)) % n
#------------------------------------------------------------------------------------------------#
def depositProfit(deposit, rate, threshold):
year = 0
while deposit < threshold:
deposit *= 1 + (rate / 100)
year += 1
return year
#------------------------------------------------------------------------------------------------#
#------------------------------------------------------------------------------------------------#
def absoluteValuesSumMinimization(a):
sums = {}
for num in a:
total = sum([abs(a[i] - num) for i in range(len(a))])
if total in sums:
sums[total] = min(num, sums[total])
else:
sums[total] = num
print(sums)
return sums[min(sums)]
import itertools
#------------------------------------------------------------------------------------------------#
def stringsRearrangement(inputArray):
permutations = itertools.permutations(inputArray)
for array in permutations:
if testArrangement(array):
return True
return False
#------------------------------------------------------------------------------------------------#
def testArrangement(array):
for i in range(len(array) - 1):
if sum([a != b for a, b in zip(array[i], array[i + 1])]) != 1:
return False
return True
#------------------------------------------------------------------------------------------------#
def extractEachKth(inputArray, k):
return [inputArray[x] for x in range(len(inputArray)) if (x + 1) % k != 0]
#------------------------------------------------------------------------------------------------#
def firstDigit(inputString):
for char in inputString:
if char.isdigit():
return char
#------------------------------------------------------------------------------------------------#
def differentSymbolsNaive(s):
return len(set(s))
#------------------------------------------------------------------------------------------------#
def arrayMaxConsecutiveSum(inputArray, k):
sums = [sum(inputArray[:k])]
for i in range(1, len(inputArray) - k + 1):
sums.append(sums[i - 1] - inputArray[i - 1] + inputArray[i + k - 1])
return max(sums)
#------------------------------------------------------------------------------------------------#
def growingPlant(upSpeed, downSpeed, desiredHeight):
height = 0
days = 1
height += upSpeed
while height < desiredHeight:
days += 1
height -= downSpeed
height += upSpeed
return days
#------------------------------------------------------------------------------------------------#
def knapsackLight(value1, weight1, value2, weight2, maxW):
if weight1 + weight2 <= maxW:
return value1 + value2
if weight1 <= maxW and (weight2 > maxW or value1 >= value2):
return value1
if weight2 <= maxW and (weight1 > maxW or value2 >= value1):
return value2
return 0
#------------------------------------------------------------------------------------------------#
def longestDigitsPrefix(inputString):
for char in range(len(inputString)):
if not inputString[char].isdigit():
return inputString[:char]
return inputString
#------------------------------------------------------------------------------------------------#
def digitDegree(n):
degree = 0
while len(str(n)) > 1:
n = sum((int(digit) for digit in str(n)))
degree += 1
return degree
#------------------------------------------------------------------------------------------------#
def bishopAndPawn(bishop, pawn):
return abs(ord(bishop[0]) - ord(pawn[0])) == abs(ord(bishop[1]) - ord(pawn[1]))
#------------------------------------------------------------------------------------------------#
def isBeautifulString(inputString):
for letter in range(ord("a"), ord("z")):
if inputString.count(chr(letter)) < inputString.count(chr(letter + 1)):
return False
return True
#------------------------------------------------------------------------------------------------#
def findEmailDomain(address):
return address[address.rfind("@") + 1 :]
#------------------------------------------------------------------------------------------------#
def buildPalindrome(st):
if st == st[::-1]: # Check for initial palindrome
return st
index = 0
subStr = st[index:]
while subStr != subStr[::-1]: # while substring is not a palindrome
index += 1
subStr = st[index:]
return st + st[index - 1 :: -1]
#------------------------------------------------------------------------------------------------#
def electionsWinners(votes, k):
winners = 0
current_winner = max(votes)
for candidate in votes:
if k > 0 and candidate + k > current_winner:
winners += 1
if k == 0 and candidate == current_winner and votes.count(candidate) == 1:
winners += 1
return winners
#------------------------------------------------------------------------------------------------#
def isMAC48Address(inputString):
hex_chars = (
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"0",
"A",
"B",
"C",
"D",
"E",
"F",
"a",
"b",
"c",
"d",
"e",
"f",
)
groups = inputString.split("-")
if len(groups) != 6:
return False
if not all((len(group) == 2 for group in groups)):
return False
if not all((group[0] in hex_chars and group[1] in hex_chars for group in groups)):
return False
return True
#------------------------------------------------------------------------------------------------#
def isDigit(symbol):
return symbol.isdigit()
#------------------------------------------------------------------------------------------------#
def lineEncoding(s):
count = 1
output = []
for char in range(1, len(s)):
if s[char] == s[char - 1]:
count += 1
else:
if count > 1:
output.append(str(count) + s[char - 1])
else:
output.append(s[char - 1])
count = 1
if s[len(s) - 1] == s[len(s) - 2]:
output.append(str(count) + s[len(s) - 1])
else:
output.append(s[len(s) - 1])
return "".join(output)
#------------------------------------------------------------------------------------------------#
def chessKnight(cell):
moves = 0
# Starting at the top left, going counter-clockwise
if ord(cell[0]) >= ord("b") and ord(cell[1]) <= ord("6"):
moves += 1
if ord(cell[0]) >= ord("c") and ord(cell[1]) <= ord("7"):
moves += 1
if ord(cell[0]) >= ord("c") and ord(cell[1]) >= ord("2"):
moves += 1
if ord(cell[0]) >= ord("b") and ord(cell[1]) >= ord("3"):
moves += 1
if ord(cell[0]) <= ord("g") and ord(cell[1]) >= ord("3"):
moves += 1
if ord(cell[0]) <= ord("f") and ord(cell[1]) >= ord("2"):
moves += 1
if ord(cell[0]) <= ord("f") and ord(cell[1]) <= ord("7"):
moves += 1
if ord(cell[0]) <= ord("g") and ord(cell[1]) <= ord("6"):
moves += 1
return moves
#------------------------------------------------------------------------------------------------#
def deleteDigit(n):
num = str(n)
highest = 0
for digit in range(len(num)):
output = num[:digit] + num[digit + 1 :]
if int(output) > int(highest):
highest = output
return int(highest)
#------------------------------------------------------------------------------------------------#
def longestWord(text):
longest = []
word = []
for char in text:
if ord("A") <= ord(char) <= ord("Z") or ord("a") <= ord(char) <= ord("z"):
word.append(char)
else:
if len(word) > len(longest):
longest = word
word = []
if len(word) > len(longest):
longest = word
return "".join(longest)
#------------------------------------------------------------------------------------------------#
def validTime(time):
groups = time.split(":")
if len(groups) != 2:
return False
if not (groups[0].isdigit() and groups[1].isdigit()):
return False
if int(groups[0]) > 23 or int(groups[1]) > 59:
return False
return True
#------------------------------------------------------------------------------------------------#
def sumUpNumbers(inputString):
total = 0
current_num = []
for char in inputString:
if char.isdigit():
current_num.append(char)
else:
if len(current_num) > 0:
num = int("".join(current_num))
total += num
current_num = []
if len(current_num) > 0:
num = int("".join(current_num))
total += num
return total
#------------------------------------------------------------------------------------------------#
def differentSquares(matrix):
squares = set()
for row in range(len(matrix) - 1):
for cell in range(len(matrix[row]) - 1):
square = (
(matrix[row][cell], matrix[row][cell + 1]),
(matrix[row + 1][cell], matrix[row + 1][cell + 1]),
)
squares.add(square)
return len(squares)
#------------------------------------------------------------------------------------------------#
def digitsProduct(product):
# New idea: add product to factors
# while max(factors) > 10: split that num into factors
if product == 0:
return 10
factors = [product]
while max(factors) > 9:
factored = findFactors(max(factors))
if factored:
factors.remove(max(factors))
factors.extend(factored)
else:
return -1
while factors.count(3) >= 2:
factors.remove(3)
factors.remove(3)
factors.append(9)
while factors.count(2) > 2:
factors.remove(2)
factors.remove(2)
factors.remove(2)
factors.append(8)
while factors.count(2) > 1:
factors.remove(2)
factors.remove(2)
factors.append(4)
while 2 in factors and 3 in factors:
factors.remove(2)
factors.remove(3)
factors.append(6)
return int("".join(map(str, sorted(factors))))
#------------------------------------------------------------------------------------------------#
def findFactors(n):
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
return i, n // i
return False
#------------------------------------------------------------------------------------------------#
def fileNaming(names):
outnames = []
for name in names:
if name in outnames:
k = 1
while "{}({})".format(name, k) in outnames:
k += 1
name = "{}({})".format(name, k)
outnames.append(name)
return outnames
#------------------------------------------------------------------------------------------------#
def messageFromBinaryCode(code):
output = []
for i in range(0, len(code), 8):
letter = chr(int(code[i : i + 8], 2))
output.append(letter)
return "".join(output)
#------------------------------------------------------------------------------------------------#
def spiralNumbers(n):
LEFT = "left"
RIGHT = "right"
UP = "up"
DOWN = "down"
direction = RIGHT
spiral = [[0 for i in range(n)] for j in range(n)]
row = 0
cell = 0
for num in range(1, (n * n) + 1):
spiral[row][cell] = num
if direction == RIGHT:
if cell != n - 1 and spiral[row][cell + 1] == 0:
cell += 1
else:
direction = DOWN
row += 1
elif direction == DOWN:
if row != n - 1 and spiral[row + 1][cell] == 0:
row += 1
else:
direction = LEFT
cell -= 1
elif direction == LEFT:
if cell != 0 and spiral[row][cell - 1] == 0:
cell -= 1
else:
direction = UP
row -= 1
elif direction == UP:
if row != 0 and spiral[row - 1][cell] == 0:
row -= 1
else:
direction = RIGHT
cell += 1
return spiral
print(spiralNumbers(5))
#------------------------------------------------------------------------------------------------#
def sudoku(grid):
match = [i for i in range(1, 10)]
for row in grid:
if sorted(row) != match:
return False
for column_index in range(9):
column = [grid[row_index][column_index] for row_index in range(9)]
if sorted(column) != match:
return False
for row in range(0, 9, 3):
for column in range(0, 9, 3):
box = []
box.extend(grid[row][column : column + 3])
box.extend(grid[row + 1][column : column + 3])
box.extend(grid[row + 2][column : column + 3])
if sorted(box) != match:
return False
return True
#------------------------------------------------------------------------------------------------#
def addTwoDigits(n):
return (n // 10) + (n % 10)
#------------------------------------------------------------------------------------------------#
def largestNumber(n):
return int("9" * n)
#------------------------------------------------------------------------------------------------#
def candies(n, m):
return (m // n) * n
#------------------------------------------------------------------------------------------------#
def seatsInTheater(nCols, nRows, col, row):
return (nCols - col + 1) * (nRows - row)
#------------------------------------------------------------------------------------------------#
def maxMultiple(divisor, bound):
for num in range(bound, 1, -1):
if num % divisor == 0:
return num
return 0
#------------------------------------------------------------------------------------------------#
def circleOfNumbers(n, firstNumber):
return (firstNumber + (n // 2)) % n
#------------------------------------------------------------------------------------------------#
def lateRide(n):
hours = n // 60
minutes = n % 60
return (hours // 10) + (hours % 10) + (minutes // 10) + (minutes % 10)
#------------------------------------------------------------------------------------------------#
def phoneCall(min1, min2_10, min11, s):
if s < min1:
return 0
if s == min1:
return 1
if s <= min1 + (min2_10 * 9):
s -= min1
return (s // min2_10) + 1
s -= min1
s -= min2_10 * 9
return (s // min11) + 10
#------------------------------------------------------------------------------------------------#
def reachNextLevel(experience, threshold, reward):
return experience + reward >= threshold
#------------------------------------------------------------------------------------------------#
def knapsackLight(value1, weight1, value2, weight2, maxW):
if weight1 + weight2 <= maxW:
return value1 + value2
if weight1 <= maxW and weight2 <= maxW:
return max(value1, value2)
if weight1 <= maxW:
return value1
if weight2 <= maxW:
return value2
return 0
#------------------------------------------------------------------------------------------------#
def extraNumber(a, b, c):
if a == b:
return c
if a == c:
return b
return a
#------------------------------------------------------------------------------------------------#
def isInfiniteProcess(a, b):
return a > b or (a % 2 != b % 2)
#------------------------------------------------------------------------------------------------#
def arithmeticExpression(a, b, c):
return a + b == c or a - b == c or a * b == c or a / b == c
#------------------------------------------------------------------------------------------------#
def tennisSet(score1, score2):
if max(score1, score2) == 6 and min(score1, score2) < 5:
return True
if 5 <= min(score1, score2) <= 6 and max(score1, score2) == 7:
return True
return False
#------------------------------------------------------------------------------------------------#
def willYou(young, beautiful, loved):
return (young and beautiful) != loved
#------------------------------------------------------------------------------------------------#
def metroCard(lastNumberOfDays):
if lastNumberOfDays == 30 or lastNumberOfDays == 28:
return [31]
return [28, 30, 31]
#------------------------------------------------------------------------------------------------#
def killKthBit(n, k):
return n & ~(2 ** (k - 1))
#------------------------------------------------------------------------------------------------#
def arrayPacking(a):
binary_array = [bin(num)[2:].rjust(8, "0") for num in a]
out_string = "".join(binary_array[::-1])
return int(out_string, 2)
#------------------------------------------------------------------------------------------------#
def rangeBitCount(a, b):
array = list(range(a, b + 1))
binary_array = [bin(num) for num in array]
count_array = [binary.count("1") for binary in binary_array]
return sum(count_array)
#------------------------------------------------------------------------------------------------#
def mirrorBits(a):
binary = bin(a)[2:]
return int(binary[::-1], 2)
#------------------------------------------------------------------------------------------------#
def secondRightmostZeroBit(n):
return 2 ** bin(n)[::-1].find("0", bin(n)[::-1].find("0") + 1)
#------------------------------------------------------------------------------------------------#
def swapAdjacentBits(n):
return ((n >> 1) & 1431655765) | ((n << 1) & 2863311530)
#------------------------------------------------------------------------------------------------#
def differentRightmostBit(n, m):
return 2 ** bin((n ^ m))[::-1].find("1")
#------------------------------------------------------------------------------------------------#
def equalPairOfBits(n, m):
return 2 ** bin(~(n ^ m))[::-1].find("1")
#------------------------------------------------------------------------------------------------#
def leastFactorial(n):
factorial = 1
index = 1
while factorial < n:
index += 1
factorial *= index
return factorial
#------------------------------------------------------------------------------------------------#
def countSumOfTwoRepresentations2(n, l, r):
count = 0
a = max(n - r, l)
b = n - a
while a <= r and a <= b:
count += 1
a += 1
b -= 1
return count
#------------------------------------------------------------------------------------------------#
def magicalWell(a, b, n):
total = 0
for i in range(n):
total += a * b
a += 1
b += 1
return total
#------------------------------------------------------------------------------------------------#
def lineUp(commands):
count = 0
smart_student = 0
dumb_student = 0
for command in commands:
if command == "L":
smart_student = (smart_student - 1) % 4
dumb_student = (dumb_student + 1) % 4
elif command == "R":
smart_student = (smart_student + 1) % 4
dumb_student = (dumb_student - 1) % 4
elif command == "A":
smart_student = (smart_student + 2) % 4
dumb_student = (dumb_student + 2) % 4
if smart_student == dumb_student:
count += 1
return count
#------------------------------------------------------------------------------------------------#
def additionWithoutCarrying(param1, param2):
# Convert numbers to strings
str1 = str(param1)
str2 = str(param2)
# Pad both to the same length with zeroes (to the left of the numbers)
length = max(len(str2), len(str1))
str1 = str1.rjust(length, "0")
str2 = str2.rjust(length, "0")
output = []
for num1, num2 in zip(str1, str2):
result = str(int(num1) + int(num2))[-1]
output.append(result)
return int("".join(output))
#------------------------------------------------------------------------------------------------#
def appleBoxes(k):
red = 0
yellow = 0
for i in range(1, k + 1, 2):
yellow += i * i
for i in range(2, k + 1, 2):
red += i * i
return red - yellow
#------------------------------------------------------------------------------------------------#
def increaseNumberRoundness(n):
string = str(n)
# Check for immediate rejection
if "0" not in string or len(string) < 2:
return False
# Since we know there's a 0, if it's not on
# the left, then we know to accept
if string[-1] != "0":
return True
# If there is only one 0, it must be at the end, so reject.
if string.count("0") == 1:
return False
# If there are any numbers between the first 0
# and the end of the string, then accept.
first_zero = string.find("0")
zero_sandwich = string[first_zero:]
return zero_sandwich.count("0") != len(zero_sandwich)
#------------------------------------------------------------------------------------------------#
def rounders(value):
length = len(str(value))
magnitude = length - 1
for i in range(length - 1):
value = int((value / 10) + 0.5)
return value * (10 ** magnitude)
#------------------------------------------------------------------------------------------------#
def candles(candlesNumber, makeNew):
totalBurned = 0
leftovers = 0
while candlesNumber > 0:
totalBurned += candlesNumber
leftovers += candlesNumber
candlesNumber = 0
candlesNumber = leftovers // makeNew
leftovers = leftovers % makeNew
return totalBurned
#------------------------------------------------------------------------------------------------#
def countBlackCells(n, m):
gcd = find_gcd(n, m)
line_cells = n + m - gcd
line_corner_cells = (gcd - 1) * 2
return line_cells + line_corner_cells
#------------------------------------------------------------------------------------------------#
def find_gcd(a, b):
while b != 0:
a, b = b, a % b
return a
#------------------------------------------------------------------------------------------------#
def createArray(size):
return [1] * size
#------------------------------------------------------------------------------------------------#
def arrayReplace(inputArray, elemToReplace, substitutionElem):
output = [
elem if elem != elemToReplace else substitutionElem for elem in inputArray
]
return output
#------------------------------------------------------------------------------------------------#
def firstReverseTry(arr):
if len(arr) < 2:
return arr
if len(arr) < 4:
return arr[::-1]
return arr[-1:] + arr[1:-1] + arr[:1]
#------------------------------------------------------------------------------------------------#
def concatenateArrays(a, b):
return a + b
#------------------------------------------------------------------------------------------------#
def removeArrayPart(inputArray, l, r):
return inputArray[:l] + inputArray[r + 1 :]
#------------------------------------------------------------------------------------------------#
def isSmooth(arr):
if arr[0] != arr[-1]:
return False
if len(arr) % 2 == 0:
middle = arr[len(arr) // 2] + arr[(len(arr) // 2) - 1]
else:
middle = arr[len(arr) // 2]
return arr[0] == middle
#------------------------------------------------------------------------------------------------#
def replaceMiddle(arr):
if len(arr) % 2 != 0:
return arr
right_middle = len(arr) // 2
middle_value = arr[right_middle] + arr[right_middle - 1]
return arr[: right_middle - 1] + [middle_value] + arr[right_middle + 1 :]
#------------------------------------------------------------------------------------------------#
def makeArrayConsecutive2(statues):
count = 0
for i in range(min(statues), max(statues)):
if i not in statues:
count += 1
return count
#------------------------------------------------------------------------------------------------#
def isPower(n):
if n == 1:
return True
a = 2
b = 2
while a ** 2 <= n:
while a ** b <= n:
if a ** b == n:
return True
b += 1
b = 2
a += 1
return False
#------------------------------------------------------------------------------------------------#
def isSumOfConsecutive2(n):
count = 0
right = 2
arr = [1, 2]
while right <= (n // 2) + 1:
total = sum(arr)
if total == n:
count += 1
del arr[0]
elif total < n:
right += 1
arr.append(right)
elif total > n:
del arr[0]
return count
#------------------------------------------------------------------------------------------------#
def squareDigitsSequence(a0):
sequence = [a0]
while sequence[-1] not in sequence[:-1]:
next_value = 0
for digit in str(sequence[-1]):
next_value += int(digit) ** 2
sequence.append(next_value)
return len(sequence)
#------------------------------------------------------------------------------------------------#
def pagesNumberingWithInk(current, numberOfDigits):
numberOfDigits -= len(str(current))
next_digits = len(str(current + 1))
while numberOfDigits >= next_digits:
current += 1
numberOfDigits -= next_digits
next_digits = len(str(current))
return current
#------------------------------------------------------------------------------------------------#
def comfortableNumbers(l, r):
count = 0
for a in range(l, r):
for b in range(a + 1, r + 1):
a_sum = sum(int(digit) for digit in str(a))
b_sum = sum(int(digit) for digit in str(b))
if b <= a + a_sum and a >= b - b_sum:
count += 1
return count
#------------------------------------------------------------------------------------------------#
def weakNumbers(n):
all_factors = [count_factors(num) for num in range(1, n + 1)]
weaknesses = []
for num, num_factors in enumerate(all_factors, 1):
weakness = 0
for factor in all_factors[:num]:
if factor > num_factors:
weakness += 1
weaknesses.append(weakness)
weakest = max(weaknesses)
return [weakest, weaknesses.count(weakest)]
#------------------------------------------------------------------------------------------------#
def count_factors(n):
factors = 0
for i in range(1, n + 1):
if n % i == 0:
factors += 1
return factors
print(weakNumbers(500))
import math
#------------------------------------------------------------------------------------------------#
def rectangleRotation(a, b):
n = a / (2 ** 0.5)
m = b / (2 ** 0.5)
points = (math.floor(n) * math.floor(m)) + (math.ceil(n) * math.ceil(m))
if math.floor(n) % 2 != math.floor(m) % 2:
points -= 1
return points
# rectangleRotation(6, 4)
print(rectangleRotation(8, 6))
# ------------------------------------------------------------------------------------------------#
# ------------------------------------------------------------------------------------------------#
def centuryFromYear(year):
return ((year - 1) // 100) + 1
# ------------------------------------------------------------------------------------------------#
def checkPalindrome(inputString):
return inputString == inputString[::-1]
# ------------------------------------------------------------------------------------------------#
def adjacentElementsProduct(inputArray):
max = inputArray[0] * inputArray[1]
for i in range(len(inputArray) - 1):
if inputArray[i] * inputArray[i + 1] > max:
max = inputArray[i] * inputArray[i + 1]
return max
# ------------------------------------------------------------------------------------------------#
def shapeArea(n):
sum = n * 2 - 1
for i in range(1, (n * 2) - 1, 2):
sum += i * 2
return sum
# ------------------------------------------------------------------------------------------------#
def makeArrayConsecutive2(statues):
return max(statues) - min(statues) - len(statues) + 1
# ------------------------------------------------------------------------------------------------#
def almostIncreasingSequence(sequence):
i = 0
while i < len(sequence) - 1:
if not sequence[i] < sequence[i + 1]:
if increasingSequence(
sequence[:i] + sequence[i + 1 :]
) or increasingSequence(sequence[: i + 1] + sequence[i + 2 :]):
return True
else:
return False
i += 1
return True
# ------------------------------------------------------------------------------------------------#
def increasingSequence(sequence):
for i in range(len(sequence) - 1):
if not sequence[i] < sequence[i + 1]:
return False
return True
# ------------------------------------------------------------------------------------------------#
def matrixElementsSum(matrix):
if len(matrix) > 1:
for row in range(1, len(matrix)):
for room in range(len(matrix[row])):
if matrix[row - 1][room] == 0:
matrix[row][room] = 0
sum = 0
for row in matrix:
for room in row:
sum += room
return sum
# ------------------------------------------------------------------------------------------------#
def allLongestStrings(inputArray):
length = max([len(word) for word in inputArray])
result = [word for word in inputArray if len(word) == length]
return result
# ------------------------------------------------------------------------------------------------#
def commonCharacterCount(s1, s2):
count = 0
word2 = list(s2)
for letter in s1:
if letter in word2:
word2.remove(letter)
count += 1
return count
# ------------------------------------------------------------------------------------------------#
def isLucky(n):
string = str(n)
top = [int(x) for x in string[: len(string) // 2]]
bottom = [int(x) for x in string[len(string) // 2 :]]
return sum(top) == sum(bottom)
# ------------------------------------------------------------------------------------------------#
def sortByHeight(a):
treePositions = [x for x in range(len(a)) if a[x] == -1]
people = sorted([x for x in a if x != -1])
for tree in treePositions:
people.insert(tree, -1)
return people
import re
# ------------------------------------------------------------------------------------------------#
def reverseParentheses(s):
while "(" in s:
match = re.search("\([^()]*\)", s)
match_string = match.group(0)[1 : len(match.group(0)) - 1]
reversed_match_string = match_string[::-1]
s = s[: match.start()] + reversed_match_string + s[match.end() :]
return s
# ------------------------------------------------------------------------------------------------#
def alternatingSums(a):
team1 = sum(a[0::2])
team2 = sum(a[1::2])
return [team1, team2]
# ------------------------------------------------------------------------------------------------#
def addBorder(picture):
picture = ["*" + string + "*" for string in picture]
picture = [("*" * len(picture[0]))] + picture + [("*" * len(picture[0]))]
return picture
# ------------------------------------------------------------------------------------------------#
def areSimilar(a, b):
diff = [i for i in range(len(a)) if a[i] != b[i]]
if len(diff) == 2:
b[diff[0]], b[diff[1]] = b[diff[1]], b[diff[0]]
return a == b
# ------------------------------------------------------------------------------------------------#
def arrayChange(inputArray):
count = 0
for i in range(1, len(inputArray)):
if inputArray[i - 1] >= inputArray[i]:
difference = inputArray[i - 1] - inputArray[i]
inputArray[i] += difference + 1
count += difference + 1
return count
# ------------------------------------------------------------------------------------------------#
def palindromeRearranging(inputString):
inputList = sorted(inputString)
foundMiddle = False
while len(inputList) > 1:
if inputList[0] == inputList[1]:
del inputList[1]
elif not foundMiddle:
foundMiddle = True
else:
return False
del inputList[0]
return len(inputList) == 0 or not foundMiddle
# ------------------------------------------------------------------------------------------------#
def areEquallyStrong(yourLeft, yourRight, friendsLeft, friendsRight):
sameHands = yourLeft == friendsLeft and yourRight == friendsRight
differentHands = yourLeft == friendsRight and yourRight == friendsLeft
return sameHands or differentHands
# ------------------------------------------------------------------------------------------------#
def arrayMaximalAdjacentDifference(inputArray):
diffs = []
for i in range(len(inputArray) - 1):
diffs.append(abs(inputArray[i] - inputArray[i + 1]))
return max(diffs)
# ------------------------------------------------------------------------------------------------#
def isIPv4Address(inputString):
strings = [string for string in inputString.split(".")]
for string in strings:
if not string.isdecimal():
return False
nums = [int(num) for num in strings]
return max(nums) <= 255 and min(nums) >= 0 and len(nums) == 4
# ------------------------------------------------------------------------------------------------#
def avoidObstacles(inputArray):
for length in range(2, max(inputArray) + 2):
done = True
jump = length
while jump < (max(inputArray) + length):
if jump in inputArray:
done = False
break
jump += length
if done:
return length
# ------------------------------------------------------------------------------------------------#
def boxBlur(image):
outImage = []
for row in range(1, len(image) - 1):
line = []
for pixel in range(1, len(image[row]) - 1):
total = (
image[row - 1][pixel - 1]
+ image[row - 1][pixel]
+ image[row - 1][pixel + 1]
+ image[row][pixel - 1]
+ image[row][pixel]
+ image[row][pixel + 1]
+ image[row + 1][pixel - 1]
+ image[row + 1][pixel]
+ image[row + 1][pixel + 1]
)
line.append(total // 9)
outImage.append(line)
return outImage
# ------------------------------------------------------------------------------------------------#
def minesweeper(matrix):
TOP = 0
BOTTOM = len(matrix) - 1
LEFT = 0
RIGHT = len(matrix[0]) - 1
outMatrix = []
for row in range(len(matrix)):
outRow = []
for cell in range(len(matrix[row])):
outRow.append(0)
if row != TOP:
outRow[cell] += matrix[row - 1][cell]
if row != BOTTOM:
outRow[cell] += matrix[row + 1][cell]
if cell != LEFT:
outRow[cell] += matrix[row][cell - 1]
if cell != RIGHT:
outRow[cell] += matrix[row][cell + 1]
if row != TOP and cell != LEFT:
outRow[cell] += matrix[row - 1][cell - 1]
if row != TOP and cell != RIGHT:
outRow[cell] += matrix[row - 1][cell + 1]
if row != BOTTOM and cell != LEFT:
outRow[cell] += matrix[row + 1][cell - 1]
if row != BOTTOM and cell != RIGHT:
outRow[cell] += matrix[row + 1][cell + 1]
outMatrix.append(outRow)
return outMatrix
# ------------------------------------------------------------------------------------------------#
def arrayReplace(inputArray, elemToReplace, substitutionElem):
return [x if x != elemToReplace else substitutionElem for x in inputArray]
# ------------------------------------------------------------------------------------------------#
def evenDigitsOnly(n):
return all(
(True if digit in ("0", "2", "4", "6", "8") else False for digit in str(n))
)
# ------------------------------------------------------------------------------------------------#
def variableName(name):
return name.replace("_", "").isalnum() and not name[0].isdigit()
# ------------------------------------------------------------------------------------------------#
def alphabeticShift(inputString):
return "".join([chr(ord(x) + 1) if x != "z" else "a" for x in inputString])
# ------------------------------------------------------------------------------------------------#
def chessBoardCellColor(cell1, cell2):
color1 = ((ord(cell1[0]) - ord("A")) + ord(cell1[1]) - ord("1")) % 2 == 0
color2 = ((ord(cell2[0]) - ord("A")) + ord(cell2[1]) - ord("1")) % 2 == 0
return color1 == color2
# ------------------------------------------------------------------------------------------------#
def circleOfNumbers(n, firstNumber):
return (firstNumber + (n / 2)) % n
# ------------------------------------------------------------------------------------------------#
def depositProfit(deposit, rate, threshold):
year = 0
while deposit < threshold:
deposit *= 1 + (rate / 100)
year += 1
return year
# ------------------------------------------------------------------------------------------------#
# ------------------------------------------------------------------------------------------------#
def absoluteValuesSumMinimization(a):
sums = {}
for num in a:
total = sum([abs(a[i] - num) for i in range(len(a))])
if total in sums:
sums[total] = min(num, sums[total])
else:
sums[total] = num
print(sums)
return sums[min(sums)]
import itertools
# ------------------------------------------------------------------------------------------------#
def stringsRearrangement(inputArray):
permutations = itertools.permutations(inputArray)
for array in permutations:
if testArrangement(array):
return True
return False
# ------------------------------------------------------------------------------------------------#
def testArrangement(array):
for i in range(len(array) - 1):
if sum([a != b for a, b in zip(array[i], array[i + 1])]) != 1:
return False
return True
# ------------------------------------------------------------------------------------------------#
def extractEachKth(inputArray, k):
return [inputArray[x] for x in range(len(inputArray)) if (x + 1) % k != 0]
# ------------------------------------------------------------------------------------------------#
def firstDigit(inputString):
for char in inputString:
if char.isdigit():
return char
# ------------------------------------------------------------------------------------------------#
def differentSymbolsNaive(s):
return len(set(s))
# ------------------------------------------------------------------------------------------------#
def arrayMaxConsecutiveSum(inputArray, k):
sums = [sum(inputArray[:k])]
for i in range(1, len(inputArray) - k + 1):
sums.append(sums[i - 1] - inputArray[i - 1] + inputArray[i + k - 1])
return max(sums)
# ------------------------------------------------------------------------------------------------#
def growingPlant(upSpeed, downSpeed, desiredHeight):
height = 0
days = 1
height += upSpeed
while height < desiredHeight:
days += 1
height -= downSpeed
height += upSpeed
return days
# ------------------------------------------------------------------------------------------------#
def knapsackLight(value1, weight1, value2, weight2, maxW):
if weight1 + weight2 <= maxW:
return value1 + value2
if weight1 <= maxW and (weight2 > maxW or value1 >= value2):
return value1
if weight2 <= maxW and (weight1 > maxW or value2 >= value1):
return value2
return 0
# ------------------------------------------------------------------------------------------------#
def longestDigitsPrefix(inputString):
for char in range(len(inputString)):
if not inputString[char].isdigit():
return inputString[:char]
return inputString
# ------------------------------------------------------------------------------------------------#
def digitDegree(n):
degree = 0
while len(str(n)) > 1:
n = sum((int(digit) for digit in str(n)))
degree += 1
return degree
# ------------------------------------------------------------------------------------------------#
def bishopAndPawn(bishop, pawn):
return abs(ord(bishop[0]) - ord(pawn[0])) == abs(ord(bishop[1]) - ord(pawn[1]))
# ------------------------------------------------------------------------------------------------#
def isBeautifulString(inputString):
for letter in range(ord("a"), ord("z")):
if inputString.count(chr(letter)) < inputString.count(chr(letter + 1)):
return False
return True
# ------------------------------------------------------------------------------------------------#
def findEmailDomain(address):
return address[address.rfind("@") + 1 :]
# ------------------------------------------------------------------------------------------------#
def buildPalindrome(st):
if st == st[::-1]: # Check for initial palindrome
return st
index = 0
subStr = st[index:]
while subStr != subStr[::-1]: # while substring is not a palindrome
index += 1
subStr = st[index:]
return st + st[index - 1 :: -1]
# ------------------------------------------------------------------------------------------------#
def electionsWinners(votes, k):
winners = 0
current_winner = max(votes)
for candidate in votes:
if k > 0 and candidate + k > current_winner:
winners += 1
if k == 0 and candidate == current_winner and votes.count(candidate) == 1:
winners += 1
return winners
# ------------------------------------------------------------------------------------------------#
def isMAC48Address(inputString):
hex_chars = (
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"0",
"A",
"B",
"C",
"D",
"E",
"F",
"a",
"b",
"c",
"d",
"e",
"f",
)
groups = inputString.split("-")
if len(groups) != 6:
return False
if not all((len(group) == 2 for group in groups)):
return False
if not all((group[0] in hex_chars and group[1] in hex_chars for group in groups)):
return False
return True
# ------------------------------------------------------------------------------------------------#
def isDigit(symbol):
return symbol.isdigit()
# ------------------------------------------------------------------------------------------------#
def lineEncoding(s):
count = 1
output = []
for char in range(1, len(s)):
if s[char] == s[char - 1]:
count += 1
else:
if count > 1:
output.append(str(count) + s[char - 1])
else:
output.append(s[char - 1])
count = 1
if s[len(s) - 1] == s[len(s) - 2]:
output.append(str(count) + s[len(s) - 1])
else:
output.append(s[len(s) - 1])
return "".join(output)
# ------------------------------------------------------------------------------------------------#
def chessKnight(cell):
moves = 0
# Starting at the top left, going counter-clockwise
if ord(cell[0]) >= ord("b") and ord(cell[1]) <= ord("6"):
moves += 1
if ord(cell[0]) >= ord("c") and ord(cell[1]) <= ord("7"):
moves += 1
if ord(cell[0]) >= ord("c") and ord(cell[1]) >= ord("2"):
moves += 1
if ord(cell[0]) >= ord("b") and ord(cell[1]) >= ord("3"):
moves += 1
if ord(cell[0]) <= ord("g") and ord(cell[1]) >= ord("3"):
moves += 1
if ord(cell[0]) <= ord("f") and ord(cell[1]) >= ord("2"):
moves += 1
if ord(cell[0]) <= ord("f") and ord(cell[1]) <= ord("7"):
moves += 1
if ord(cell[0]) <= ord("g") and ord(cell[1]) <= ord("6"):
moves += 1
return moves
# ------------------------------------------------------------------------------------------------#
def deleteDigit(n):
num = str(n)
highest = 0
for digit in range(len(num)):
output = num[:digit] + num[digit + 1 :]
if int(output) > int(highest):
highest = output
return int(highest)
# ------------------------------------------------------------------------------------------------#
def longestWord(text):
longest = []
word = []
for char in text:
if ord("A") <= ord(char) <= ord("Z") or ord("a") <= ord(char) <= ord("z"):
word.append(char)
else:
if len(word) > len(longest):
longest = word
word = []
if len(word) > len(longest):
longest = word
return "".join(longest)
# ------------------------------------------------------------------------------------------------#
def validTime(time):
groups = time.split(":")
if len(groups) != 2:
return False
if not (groups[0].isdigit() and groups[1].isdigit()):
return False
if int(groups[0]) > 23 or int(groups[1]) > 59:
return False
return True
# ------------------------------------------------------------------------------------------------#
def sumUpNumbers(inputString):
total = 0
current_num = []
for char in inputString:
if char.isdigit():
current_num.append(char)
else:
if len(current_num) > 0:
num = int("".join(current_num))
total += num
current_num = []
if len(current_num) > 0:
num = int("".join(current_num))
total += num
return total
# ------------------------------------------------------------------------------------------------#
def differentSquares(matrix):
squares = set()
for row in range(len(matrix) - 1):
for cell in range(len(matrix[row]) - 1):
square = (
(matrix[row][cell], matrix[row][cell + 1]),
(matrix[row + 1][cell], matrix[row + 1][cell + 1]),
)
squares.add(square)
return len(squares)
# ------------------------------------------------------------------------------------------------#
def digitsProduct(product):
# New idea: add product to factors
# while max(factors) > 10: split that num into factors
if product == 0:
return 10
factors = [product]
while max(factors) > 9:
factored = findFactors(max(factors))
if factored:
factors.remove(max(factors))
factors.extend(factored)
else:
return -1
while factors.count(3) >= 2:
factors.remove(3)
factors.remove(3)
factors.append(9)
while factors.count(2) > 2:
factors.remove(2)
factors.remove(2)
factors.remove(2)
factors.append(8)
while factors.count(2) > 1:
factors.remove(2)
factors.remove(2)
factors.append(4)
while 2 in factors and 3 in factors:
factors.remove(2)
factors.remove(3)
factors.append(6)
return int("".join(map(str, sorted(factors))))
# ------------------------------------------------------------------------------------------------#
def findFactors(n):
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
return i, n // i
return False
# ------------------------------------------------------------------------------------------------#
def fileNaming(names):
outnames = []
for name in names:
if name in outnames:
k = 1
while "{}({})".format(name, k) in outnames:
k += 1
name = "{}({})".format(name, k)
outnames.append(name)
return outnames
# ------------------------------------------------------------------------------------------------#
def messageFromBinaryCode(code):
output = []
for i in range(0, len(code), 8):
letter = chr(int(code[i : i + 8], 2))
output.append(letter)
return "".join(output)
# ------------------------------------------------------------------------------------------------#
def spiralNumbers(n):
LEFT = "left"
RIGHT = "right"
UP = "up"
DOWN = "down"
direction = RIGHT
spiral = [[0 for i in range(n)] for j in range(n)]
row = 0
cell = 0
for num in range(1, (n * n) + 1):
spiral[row][cell] = num
if direction == RIGHT:
if cell != n - 1 and spiral[row][cell + 1] == 0:
cell += 1
else:
direction = DOWN
row += 1
elif direction == DOWN:
if row != n - 1 and spiral[row + 1][cell] == 0:
row += 1
else:
direction = LEFT
cell -= 1
elif direction == LEFT:
if cell != 0 and spiral[row][cell - 1] == 0:
cell -= 1
else:
direction = UP
row -= 1
elif direction == UP:
if row != 0 and spiral[row - 1][cell] == 0:
row -= 1
else:
direction = RIGHT
cell += 1
return spiral
print(spiralNumbers(5))
# ------------------------------------------------------------------------------------------------#
def sudoku(grid):
match = [i for i in range(1, 10)]
for row in grid:
if sorted(row) != match:
return False
for column_index in range(9):
column = [grid[row_index][column_index] for row_index in range(9)]
if sorted(column) != match:
return False
for row in range(0, 9, 3):
for column in range(0, 9, 3):
box = []
box.extend(grid[row][column : column + 3])
box.extend(grid[row + 1][column : column + 3])
box.extend(grid[row + 2][column : column + 3])
if sorted(box) != match:
return False
return True
# ------------------------------------------------------------------------------------------------#
def addTwoDigits(n):
return (n // 10) + (n % 10)
# ------------------------------------------------------------------------------------------------#
def largestNumber(n):
return int("9" * n)
# ------------------------------------------------------------------------------------------------#
def candies(n, m):
return (m // n) * n
# ------------------------------------------------------------------------------------------------#
def seatsInTheater(nCols, nRows, col, row):
return (nCols - col + 1) * (nRows - row)
# ------------------------------------------------------------------------------------------------#
def maxMultiple(divisor, bound):
for num in range(bound, 1, -1):
if num % divisor == 0:
return num
return 0
# ------------------------------------------------------------------------------------------------#
def circleOfNumbers(n, firstNumber):
return (firstNumber + (n // 2)) % n
# ------------------------------------------------------------------------------------------------#
def lateRide(n):
hours = n // 60
minutes = n % 60
return (hours // 10) + (hours % 10) + (minutes // 10) + (minutes % 10)
# ------------------------------------------------------------------------------------------------#
def phoneCall(min1, min2_10, min11, s):
if s < min1:
return 0
if s == min1:
return 1
if s <= min1 + (min2_10 * 9):
s -= min1
return (s // min2_10) + 1
s -= min1
s -= min2_10 * 9
return (s // min11) + 10
# ------------------------------------------------------------------------------------------------#
def reachNextLevel(experience, threshold, reward):
return experience + reward >= threshold
# ------------------------------------------------------------------------------------------------#
def knapsackLight(value1, weight1, value2, weight2, maxW):
if weight1 + weight2 <= maxW:
return value1 + value2
if weight1 <= maxW and weight2 <= maxW:
return max(value1, value2)
if weight1 <= maxW:
return value1
if weight2 <= maxW:
return value2
return 0
# ------------------------------------------------------------------------------------------------#
def extraNumber(a, b, c):
if a == b:
return c
if a == c:
return b
return a
# ------------------------------------------------------------------------------------------------#
def isInfiniteProcess(a, b):
return a > b or (a % 2 != b % 2)
# ------------------------------------------------------------------------------------------------#
def arithmeticExpression(a, b, c):
return a + b == c or a - b == c or a * b == c or a / b == c
# ------------------------------------------------------------------------------------------------#
def tennisSet(score1, score2):
if max(score1, score2) == 6 and min(score1, score2) < 5:
return True
if 5 <= min(score1, score2) <= 6 and max(score1, score2) == 7:
return True
return False
# ------------------------------------------------------------------------------------------------#
def willYou(young, beautiful, loved):
return (young and beautiful) != loved
# ------------------------------------------------------------------------------------------------#
def metroCard(lastNumberOfDays):
if lastNumberOfDays == 30 or lastNumberOfDays == 28:
return [31]
return [28, 30, 31]
# ------------------------------------------------------------------------------------------------#
def killKthBit(n, k):
return n & ~(2 ** (k - 1))
# ------------------------------------------------------------------------------------------------#
def arrayPacking(a):
binary_array = [bin(num)[2:].rjust(8, "0") for num in a]
out_string = "".join(binary_array[::-1])
return int(out_string, 2)
# ------------------------------------------------------------------------------------------------#
def rangeBitCount(a, b):
array = list(range(a, b + 1))
binary_array = [bin(num) for num in array]
count_array = [binary.count("1") for binary in binary_array]
return sum(count_array)
# ------------------------------------------------------------------------------------------------#
def mirrorBits(a):
binary = bin(a)[2:]
return int(binary[::-1], 2)
# ------------------------------------------------------------------------------------------------#
def secondRightmostZeroBit(n):
return 2 ** bin(n)[::-1].find("0", bin(n)[::-1].find("0") + 1)
# ------------------------------------------------------------------------------------------------#
def swapAdjacentBits(n):
return ((n >> 1) & 1431655765) | ((n << 1) & 2863311530)
# ------------------------------------------------------------------------------------------------#
def differentRightmostBit(n, m):
return 2 ** bin((n ^ m))[::-1].find("1")
# ------------------------------------------------------------------------------------------------#
def equalPairOfBits(n, m):
return 2 ** bin(~(n ^ m))[::-1].find("1")
# ------------------------------------------------------------------------------------------------#
def leastFactorial(n):
factorial = 1
index = 1
while factorial < n:
index += 1
factorial *= index
return factorial
# ------------------------------------------------------------------------------------------------#
def countSumOfTwoRepresentations2(n, l, r):
count = 0
a = max(n - r, l)
b = n - a
while a <= r and a <= b:
count += 1
a += 1
b -= 1
return count
# ------------------------------------------------------------------------------------------------#
def magicalWell(a, b, n):
total = 0
for i in range(n):
total += a * b
a += 1
b += 1
return total
# ------------------------------------------------------------------------------------------------#
def lineUp(commands):
count = 0
smart_student = 0
dumb_student = 0
for command in commands:
if command == "L":
smart_student = (smart_student - 1) % 4
dumb_student = (dumb_student + 1) % 4
elif command == "R":
smart_student = (smart_student + 1) % 4
dumb_student = (dumb_student - 1) % 4
elif command == "A":
smart_student = (smart_student + 2) % 4
dumb_student = (dumb_student + 2) % 4
if smart_student == dumb_student:
count += 1
return count
# ------------------------------------------------------------------------------------------------#
def additionWithoutCarrying(param1, param2):
# Convert numbers to strings
str1 = str(param1)
str2 = str(param2)
# Pad both to the same length with zeroes (to the left of the numbers)
length = max(len(str2), len(str1))
str1 = str1.rjust(length, "0")
str2 = str2.rjust(length, "0")
output = []
for num1, num2 in zip(str1, str2):
result = str(int(num1) + int(num2))[-1]
output.append(result)
return int("".join(output))
# ------------------------------------------------------------------------------------------------#
def appleBoxes(k):
red = 0
yellow = 0
for i in range(1, k + 1, 2):
yellow += i * i
for i in range(2, k + 1, 2):
red += i * i
return red - yellow
# ------------------------------------------------------------------------------------------------#
def increaseNumberRoundness(n):
string = str(n)
# Check for immediate rejection
if "0" not in string or len(string) < 2:
return False
# Since we know there's a 0, if it's not on
# the left, then we know to accept
if string[-1] != "0":
return True
# If there is only one 0, it must be at the end, so reject.
if string.count("0") == 1:
return False
# If there are any numbers between the first 0
# and the end of the string, then accept.
first_zero = string.find("0")
zero_sandwich = string[first_zero:]
return zero_sandwich.count("0") != len(zero_sandwich)
# ------------------------------------------------------------------------------------------------#
def rounders(value):
length = len(str(value))
magnitude = length - 1
for i in range(length - 1):
value = int((value / 10) + 0.5)
return value * (10 ** magnitude)
# ------------------------------------------------------------------------------------------------#
def candles(candlesNumber, makeNew):
totalBurned = 0
leftovers = 0
while candlesNumber > 0:
totalBurned += candlesNumber
leftovers += candlesNumber
candlesNumber = 0
candlesNumber = leftovers // makeNew
leftovers = leftovers % makeNew
return totalBurned
# ------------------------------------------------------------------------------------------------#
def countBlackCells(n, m):
gcd = find_gcd(n, m)
line_cells = n + m - gcd
line_corner_cells = (gcd - 1) * 2
return line_cells + line_corner_cells
# ------------------------------------------------------------------------------------------------#
def find_gcd(a, b):
while b != 0:
a, b = b, a % b
return a
# ------------------------------------------------------------------------------------------------#
def createArray(size):
return [1] * size
# ------------------------------------------------------------------------------------------------#
def arrayReplace(inputArray, elemToReplace, substitutionElem):
output = [
elem if elem != elemToReplace else substitutionElem for elem in inputArray
]
return output
# ------------------------------------------------------------------------------------------------#
def firstReverseTry(arr):
if len(arr) < 2:
return arr
if len(arr) < 4:
return arr[::-1]
return arr[-1:] + arr[1:-1] + arr[:1]
# ------------------------------------------------------------------------------------------------#
def concatenateArrays(a, b):
return a + b
# ------------------------------------------------------------------------------------------------#
def removeArrayPart(inputArray, l, r):
return inputArray[:l] + inputArray[r + 1 :]
# ------------------------------------------------------------------------------------------------#
def isSmooth(arr):
if arr[0] != arr[-1]:
return False
if len(arr) % 2 == 0:
middle = arr[len(arr) // 2] + arr[(len(arr) // 2) - 1]
else:
middle = arr[len(arr) // 2]
return arr[0] == middle
# ------------------------------------------------------------------------------------------------#
def replaceMiddle(arr):
if len(arr) % 2 != 0:
return arr
right_middle = len(arr) // 2
middle_value = arr[right_middle] + arr[right_middle - 1]
return arr[: right_middle - 1] + [middle_value] + arr[right_middle + 1 :]
# ------------------------------------------------------------------------------------------------#
def makeArrayConsecutive2(statues):
count = 0
for i in range(min(statues), max(statues)):
if i not in statues:
count += 1
return count
# ------------------------------------------------------------------------------------------------#
def isPower(n):
if n == 1:
return True
a = 2
b = 2
while a ** 2 <= n:
while a ** b <= n:
if a ** b == n:
return True
b += 1
b = 2
a += 1
return False
# ------------------------------------------------------------------------------------------------#
def isSumOfConsecutive2(n):
count = 0
right = 2
arr = [1, 2]
while right <= (n // 2) + 1:
total = sum(arr)
if total == n:
count += 1
del arr[0]
elif total < n:
right += 1
arr.append(right)
elif total > n:
del arr[0]
return count
# ------------------------------------------------------------------------------------------------#
def squareDigitsSequence(a0):
sequence = [a0]
while sequence[-1] not in sequence[:-1]:
next_value = 0
for digit in str(sequence[-1]):
next_value += int(digit) ** 2
sequence.append(next_value)
return len(sequence)
# ------------------------------------------------------------------------------------------------#
def pagesNumberingWithInk(current, numberOfDigits):
numberOfDigits -= len(str(current))
next_digits = len(str(current + 1))
while numberOfDigits >= next_digits:
current += 1
numberOfDigits -= next_digits
next_digits = len(str(current))
return current
# ------------------------------------------------------------------------------------------------#
def comfortableNumbers(l, r):
count = 0
for a in range(l, r):
for b in range(a + 1, r + 1):
a_sum = sum(int(digit) for digit in str(a))
b_sum = sum(int(digit) for digit in str(b))
if b <= a + a_sum and a >= b - b_sum:
count += 1
return count
# ------------------------------------------------------------------------------------------------#
def weakNumbers(n):
all_factors = [count_factors(num) for num in range(1, n + 1)]
weaknesses = []
for num, num_factors in enumerate(all_factors, 1):
weakness = 0
for factor in all_factors[:num]:
if factor > num_factors:
weakness += 1
weaknesses.append(weakness)
weakest = max(weaknesses)
return [weakest, weaknesses.count(weakest)]
# ------------------------------------------------------------------------------------------------#
def count_factors(n):
factors = 0
for i in range(1, n + 1):
if n % i == 0:
factors += 1
return factors
print(weakNumbers(500))
import math
```py
Given an array A return an output array B of size `[A.length - 2]` where B[i] = 1 if sides A[i],A[i+1] and A[i+2] form a triangle. Otherwise, set B[i] = 0.\
ex. A = [1, 2, 2, 5, 5, 4] should return B = [1,0,0,1]
Given two strings a and b, merge the strings so that the letters are added in alternating order starting with string a. If one string is longer than the other, then append the letters to the end of the merged string.\
ex. "abcd", "efghi" -> "aebfcgdhi"\
ex. "", "abcd" -> "abcd"\
ex. "abcdefg", "zxy" -> "azbxycdefg"\
Pretty easy. Just interlace them like a merge sort.
Given a string s, return the longest and lexicographically smallest palindromic string that can be formed from the characters.\
ex. "abbaa" -> "abba"\
ex. "adeadeadevue" -> "adeeaeeda"
```
def smallestPalindrome(s):
if not s:
return s
res = []
counts = collections.Counter(s)
alphabet = "abcdefghijklmnopqrstuvwxyz"
middle_letter = ""
for letter in alphabet:
count = counts.get(letter, 0)
if count > 0:
if count % 2 == 1 and middle_letter == "":
middle_letter = letter
res.append(letter * ((count - 1) // 2))
else:
res.append(letter * (count // 2))
first= "".join(sb)
return first + middle_letter + first[::-1]
```
* * * * *
Easy-Medium:\
Given a list of strings `string_list` and a list of words `words`, determine whether each word in `words`\
can be formed as a concatenation of consecutive strings in `string_list` starting with index 0.\
ex. word = "oneTwoThree", string_list = ["one", "Three", "Two"] is false because the words aren't consecutive in string_list\
ex. word = "one", string_list = ["one", "Three", "Two"] is True because the concatenation stops at the first index in string_list\
ex. word = "one", string_list = ["One", "one", "Two"] is False because the concatenation doesn't start at 0.\
Just use the base idea from the LeetCode problem and then modify it for the consecutive concatenation requirement. It's actually easier than the LeetCode problem.
* * * * *
Medium:
Beauty of a matrix.\
Given an n x n matrix `M` and an int `k` where `n % k == 0`, divide the M into blocks of size `k x k` starting with the top left corner. i.e. a 9x9 matrix turns into 9 3x3s. The *beauty* of a block is the smallest positive number missing from a block. Rearrange `M` so that the blocks with the lowest beauty come before those with higher beauty (top left to bottom right). In a tie, you should place the block that came first in M before the other block.\
`Just do what the problem says. Make an array A = [(int beauty, int[][] block)...] and a stable sorting algorithm will handle things. Then glue the blocks back into the matrix.`
Rotate matrix around diagonals.\
Given an n x n matrix `M`, where n is odd and n > 1, and an integer `k`, rotate `M` counterclockwise `k` times which are not on the main diagonal or on the diagonal from the top right to the bottom left.\
Return the new matrix.\
Ex. I put *s to show which elements are fixed on the diagonals.
```
[*1*, 2, 3, 4, *5*]
[6, *7*, 8, *9*, 10]
[11, 12, *13*, 14, 15]
[16, *17*, 18, *19*, 20]
[*21*, 22, 23, 24, *25*]
```
Rotates to:
```
[*1*, 16, 11, 6, *5*]
[22, *7*, 12, *9*, 2]
[23, 18, *13*, 8, 3]
[24, *17*, 14, *19*, 4]
[*21*, 20, 15, 10, *25*]
```
Just do the obvious % 4 and then rotate a deepcopy like the LeetCode problem <https://leetcode.com/problems/rotate-image/discuss/18872/A-common-method-to-rotate-the-image>, but you check whether the element is on the diagonal before you reverse it or transpose it.
* * * * *
Medium/Hard:\
Given two arrays `a` and `b` of equal length, the absolute sum difference is the `sum Math.abs(a[i] - b[i])`. Replace one number in `a` with any number in `a` to minimize the absolute sum difference.
---
I tried answering all questions. Find them in serial order.\
I have tried giving credits wherever I used other's solution
```
# Author - https://leetcode.com/techdude/
# Question - 1
"""
Given an array A return an output array B of size [A.length - 2] where B[i] = 1 if sides
A[i],A[i+1] and A[i+2] form a triangle. Otherwise, set B[i] = 0.
ex. A = [1, 2, 2, 5, 5, 4] should return B = [1,0,0,1]
"""
A = [1, 2, 2, 5, 5, 4]
def validTriangle(A):
B = []; i=0
while i < len(A) - 2:
a,b,c = A[i], A[i+1], A[i+2]
if a+b>c and b+c>a and a+c>b:
B.append(1)
else:
B.append(0)
i += 1
return B
print(validTriangle(A))
print('*****************************************')
"""
Given two strings a and b, merge the strings so that the letters are added in alternating order
starting with string a. If one string is longer than the other, then append the letters to the end of the merged string.
ex. "abcd", "efghi" -> "aebfcgdhi"
ex. "", "abcd" -> "abcd"
ex. "abcdefg", "zxy" -> "azbxycdefg"
"""
a='abcd'
b = 'efghi'
def mergeStrings(a,b):
res = ''
for l1,l2 in zip(a,b):
res += l1 + l2
if len(a) < len(b):
res += b[len(a):]
elif len(a) > len(b):
res += a[len(b):]
return res
print(mergeStrings(a,b)) # aebfcgdhi
a=''; b='abcd'
print(mergeStrings(a,b)) # abcd
a="abcdefg"; b="zxy"
print(mergeStrings(a,b)) # azbxcydefg
print('*****************************************')
"""
Given a string s, return the longest and lexicographically smallest palindromic string
that can be formed from the characters.
ex. "abbaa" -> "abba"
ex. "adeadeadevue" -> "adeeaeeda"
"""
import collections
def smallestPalindrome(str):
store = collections.Counter(str)
lexic_store = [[key,val] for key,val in sorted(store.items(), key=lambda x:x[0]) if val > 1]
lexic_store_single = [[key,val] for key,val in sorted(store.items(), key=lambda x:x[0]) if val % 2 == 1][0][0]
res = ''
for key,val in lexic_store:
res += (key * int(val/2))
return res + lexic_store_single + res[::-1]
str = "abbaa"
print(smallestPalindrome(str))
str = "adeadeadevue"
print(smallestPalindrome(str))
# OP's solution - similar :)
def smallestPalindromeOP(s):
if not s:
return s
res = []
counts = collections.Counter(s)
alphabet = "abcdefghijklmnopqrstuvwxyz"
middle_letter = ""
for letter in alphabet:
count = counts.get(letter, 0)
if count > 0:
if count % 2 == 1 and middle_letter == "":
middle_letter = letter
res.append(letter * ((count - 1) // 2))
else:
res.append(letter * (count // 2))
first = "".join(res)
return first + middle_letter + first[::-1]
print(smallestPalindromeOP('abbaa'))
print('*****************************************')
# Easy - Medium
"""
Given a list of strings string_list and a list of words words, determine whether each word in words
can be formed as a concatenation of consecutive strings in string_list starting with index 0.
ex. word = "oneTwoThree", string_list = ["one", "Three", "Two"] is false because the words aren't consecutive in string_list
ex. word = "one", string_list = ["one", "Three", "Two"] is True because the concatenation stops at the first index in string_list
ex. word = "one", string_list = ["One", "one", "Two"] is False because the concatenation doesn't start at 0.
Just use the base idea from the LeetCode problem and then modify it for the consecutive concatenation requirement.
It's actually easier than the LeetCode problem.
"""
word = "one"; string_list = ["one", "Three", "Two"]
def isConcatenate(word, string_list):
res = ''
for w in string_list:
res += w
if res == word:
return True
return False
print(isConcatenate(word, string_list))
print('*****************************************')
# Medium
"""
Beauty of a matrix.
Given an n x n matrix M and an int k where n % k == 0,
divide the M into blocks of size k x k starting with the top left corner. i.e.
a 9x9 matrix turns into 9 3x3s. The beauty of a block is the smallest positive number missing
from a block. Rearrange M so that the blocks with the lowest beauty come before those with
higher beauty (top left to bottom right).
In a tie, you should place the block that came first in M before the other block.
"""
matrix = [
[1,2,2,3],
[3,4,10,4],
[2,10,1,2],
[5,4,4,5],
]
size = 2 # 2 x 2
def get_beauty(sub_matrix):
sm = sorted(sub_matrix)
magic_num = 1
for num in sm:
if num > magic_num:
return magic_num
magic_num += 1
return magic_num
def magic_number(matrix):
sub_matrices = []
serial = 0
for j in range(size):
for i in range(size):
x, y = j * len(matrix)//size, i * len(matrix)//size
sub_matrix = []
for p in range(size):
for q in range(size):
sub_matrix.append(matrix[x+p][y+q])
beauty_num = get_beauty(sub_matrix)
sub_matrices.append([sub_matrix, beauty_num, serial])
serial += 1
sub_matrices.sort(key=lambda x: (x[1],x[2]))
serial = 0
for j in range(size):
for i in range(size):
x, y = j * len(matrix)//size, i * len(matrix)//size
iter1 = iter(sub_matrices[serial][0])
for p in range(size):
for q in range(size):
matrix[x+p][y+q] = next(iter1)
serial += 1
for row in matrix:
print(row)
return matrix
magic_number(matrix)
print('*****************************************')
"""
Rotate matrix around diagonals.
Given an n x n matrix M, where n is odd and n > 1, and an integer k,
rotate M counterclockwise k times which are not on the main diagonal or
on the diagonal from the top right to the bottom left.
Return the new matrix.
Ex. I put *s to show which elements are fixed on the diagonals.
[*1*, 2, 3, 4, *5*]
[6, *7*, 8, *9*, 10]
[11, 12, *13*, 14, 15]
[16, *17*, 18, *19*, 20]
[*21*, 22, 23, 24, *25*]
Rotates to:
[*1*, 16, 11, 6, *5*]
[22, *7*, 12, *9*, 2]
[23, 18, *13*, 8, 3]
[24, *17*, 14, *19*, 4]
[*21*, 20, 15, 10, *25*]
"""
# Brilliant method - https://leetcode.com/problems/rotate-image/discuss/18872/A-common-method-to-rotate-the-image
# * clockwise rotate
# * first reverse up to down, then swap the symmetry
# * 1 2 3 7 8 9 7 4 1
# * 4 5 6 => 4 5 6 => 8 5 2
# * 7 8 9 1 2 3 9 6 3
matrix = [
[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25],
]
def reverseImage(matrix):
start, end = 0, len(matrix)-1
while start < end:
for i in range(len(matrix)):
if start != i and end!= i:
matrix[start][i], matrix[end][i] = matrix[end][i], matrix[start][i]
start += 1
end -= 1
return matrix
def swapMatrix(matrix):
for i in range(len(matrix)):
for j in range(i+1, len(matrix)):
if i != j and i+j != len(matrix) -1:
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
return matrix
def rotateImage(matrix, num_rotate):
num_rotate = num_rotate % 4 # because 4 rotations = original image
for _ in range(num_rotate):
matrix = reverseImage(matrix)
matrix = swapMatrix(matrix)
return matrix
print(rotateImage(matrix, 17))
# Medium/Hard
"""
Given two arrays a and b of equal length,
the absolute sum difference is the sum Math.abs(a[i] - b[i]).
Replace one number in a with any number in a to minimize the absolute sum difference.
ref: https://leetcode.com/discuss/interview-question/949484/codesignal-gca-i-have-a-question
idea: https://leetcode.com/discuss/interview-question/949484/CodeSignal-GCA-(I-have-a-question)/779839
"""
arr1 = [1,2,3,4]
arr2 = [1,2,4,0]
def find_in_arr1(num, arr):
return min(arr, key=lambda x:abs(x[0]-num))[1]
def minAbsSumDiff(arr1, arr2):
sorted_arr1 = [(num,i) for i, num in enumerate(sorted(arr1))]
min_diff_sum = orig_sum_diff = sum([abs(num1 - num2) for num1, num2 in zip(arr1, arr2)])
for i, num in enumerate(arr1):
index = find_in_arr1(arr2[i], sorted_arr1)
if index != i:
min_diff_sum = min(min_diff_sum, orig_sum_diff - (abs(arr1[i]-arr2[i])) + (abs(arr1[index] - arr2[i])))
return min_diff_sum
print(minAbsSumDiff(arr1, arr2))
```
Python - beauty of matrix
```
def firstMissingPositive(nums):
n = len(nums)
if 1 not in nums:
return 1
if len(nums)==1:
return 2
for i, num in enumerate(nums):
if num<=0 or num>n:
nums[i] = 1
for i, num in enumerate(nums):
a = abs(nums[i])
if a==n:
nums[0] = -abs(nums[0])
else:
nums[a] = -abs(nums[a])
for i in range(1, len(nums)):
if nums[i]>0:
return i
if nums[0]>0:
return n
return n+1
def beautyOfMatrix(matrix, k):
n = len(matrix)
new_n = n//k
lst = [[] for j in range(new_n*new_n)]
for i in range(len(matrix)):
for j in range(len(matrix[0])):
idx = j//new_n + (i//new_n)*new_n
lst[idx].append(matrix[i][j])
missing =[]
for i in range(len(lst)):
mis_pos = firstMissingPositive(copy.deepcopy(lst[i]))
missing.append((mis_pos, i))
missing = sorted(missing)
n_box_idx = 0
for i in range(len(missing)):
box_idx = missing[i][1]
for j in range(len(lst[box_idx])):
r = (n_box_idx//new_n)*new_n + j//new_n
c = (n_box_idx%new_n)*new_n + j%new_n
matrix[r][c] = lst[box_idx][j]
n_box_idx += 1
return matrix
```
def max_k_occurences(sequence,word):
k = 1
while word*k in sequence:
k += 1
return k-1
l = []
for i in words:
l.append(max_k_occurences(sequence, i))
print l
class Solution:
def arrayShift(self, A):
n = len(A)
start = A[0]
cyclicShift = True
for a in A:
if a != start:
cyclicShift = False
break
start = (start + 1) % n
if start == 0:
start = n
if cyclicShift:
return True
start = A[0]
for a in A:
if a != start:
return False
start = (start - 1) % n
if start == 0:
start = n
return True
if __name__ == "__main__":
s = Solution()
A = [5, 1, 2, 3, 4]
print(s.arrayShift(A))
A = [4, 3, 2, 1, 5]
print(s.arrayShift(A))
A = [2, 3, 4, 5, 1]
print(s.arrayShift(A))
A = [2, 1, 5, 4, 3]
print(s.arrayShift(A))
A = [5, 2, 2, 3, 4]
print(s.arrayShift(A))
A = [4, 2, 2, 1, 5]
print(s.arrayShift(A))
def add(param1, param2):
return param1 + param2
def centuryFromYear(year):
return ((year-1) // 100) + 1
def checkPalindrome(inputString):
return inputString == inputString[::-1]
def adjacentElementsProduct(inputArray):
max = inputArray[0] * inputArray[1]
for i in range(len(inputArray) - 1):
if inputArray[i] * inputArray[i+1] > max:
max = inputArray[i] * inputArray[i+1]
return max
def shapeArea(n):
sum = n*2 - 1
for i in range(1, (n*2)-1, 2):
sum += i*2
return sum
def makeArrayConsecutive2(statues):
return max(statues) - min(statues) - len(statues) + 1
def almostIncreasingSequence(sequence):
i = 0
while i < len(sequence) - 1:
if not sequence[i] < sequence[i + 1]:
if increasingSequence(sequence[:i] + sequence[i+1:]) or \
increasingSequence(sequence[:i+1] + sequence[i+2:]):
return True
else:
return False
i += 1
return True
def increasingSequence(sequence):
for i in range(len(sequence) - 1):
if not sequence[i] < sequence[i + 1]:
return False
return True
def matrixElementsSum(matrix):
if len(matrix) > 1:
for row in range(1, len(matrix)):
for room in range(len(matrix[row])):
if matrix[row - 1][room] == 0:
matrix[row][room] = 0
sum = 0
for row in matrix:
for room in row:
sum += room
return sum
def allLongestStrings(inputArray):
length = max([len(word) for word in inputArray])
result = [word for word in inputArray if len(word) == length]
return result
def commonCharacterCount(s1, s2):
count = 0
word2 = list(s2)
for letter in s1:
if letter in word2:
word2.remove(letter)
count += 1
return count
def isLucky(n):
string = str(n)
top = [int(x) for x in string[:len(string)//2]]
bottom = [int(x) for x in string[len(string)//2:]]
return sum(top) == sum(bottom)
def sortByHeight(a):
treePositions = [x for x in range(len(a)) if a[x] == -1]
people = sorted([x for x in a if x != -1])
for tree in treePositions:
people.insert(tree, -1)
return people
import re
def reverseParentheses(s):
while "(" in s:
match = re.search("\([^()]*\)", s)
match_string = match.group(0)[1: len(match.group(0)) - 1]
reversed_match_string = match_string[::-1]
s = s[:match.start()] + reversed_match_string + s[match.end():]
return s
def alternatingSums(a):
team1 = sum(a[0::2])
team2 = sum(a[1::2])
return [team1, team2]
def addBorder(picture):
picture = ["*" + string + "*" for string in picture]
picture = [("*" * len(picture[0]))] + picture + [("*" * len(picture[0]))]
return picture
def areSimilar(a, b):
diff = [i for i in range(len(a)) if a[i] != b[i]]
if len(diff) == 2:
b[diff[0]], b[diff[1]] = b[diff[1]], b[diff[0]]
return a == b
def arrayChange(inputArray):
count = 0
for i in range(1, len(inputArray)):
if inputArray[i-1] >= inputArray[i]:
difference = inputArray[i-1] - inputArray[i]
inputArray[i] += difference + 1
count += difference + 1
return count
def palindromeRearranging(inputString):
inputList = sorted(inputString)
foundMiddle = False
while len(inputList) > 1:
if inputList[0] == inputList[1]:
del inputList[1]
elif not foundMiddle:
foundMiddle = True
else:
return False
del inputList[0]
return len(inputList) == 0 or not foundMiddle
def areEquallyStrong(yourLeft, yourRight, friendsLeft, friendsRight):
sameHands = yourLeft == friendsLeft and yourRight == friendsRight
differentHands = yourLeft == friendsRight and yourRight == friendsLeft
return sameHands or differentHands
def arrayMaximalAdjacentDifference(inputArray):
diffs = []
for i in range(len(inputArray) - 1):
diffs.append(abs(inputArray[i] - inputArray[i + 1]))
return max(diffs)
def isIPv4Address(inputString):
strings = [string for string in inputString.split('.')]
for string in strings:
if not string.isdecimal():
return False
nums = [int(num) for num in strings]
return max(nums) <= 255 and min(nums) >= 0 and len(nums) == 4
def avoidObstacles(inputArray):
for length in range(2, max(inputArray) + 2):
done = True
jump = length
while jump < (max(inputArray) + length):
if jump in inputArray:
done = False
break
jump += length
if done:
return length
def boxBlur(image):
outImage = []
for row in range(1, len(image) - 1):
line = []
for pixel in range(1, len(image[row]) - 1):
total = (image[row - 1][pixel - 1]
+ image[row - 1][pixel]
+ image[row - 1][pixel + 1]
+ image[row][pixel - 1]
+ image[row][pixel]
+ image[row][pixel + 1]
+ image[row + 1][pixel - 1]
+ image[row + 1][pixel]
+ image[row + 1][pixel + 1])
line.append(total // 9)
outImage.append(line)
return outImage
def minesweeper(matrix):
TOP = 0
BOTTOM = len(matrix) - 1
LEFT = 0
RIGHT = len(matrix[0]) - 1
outMatrix = []
for row in range(len(matrix)):
outRow = []
for cell in range(len(matrix[row])):
outRow.append(0)
if row != TOP:
outRow[cell] += matrix[row - 1][cell]
if row != BOTTOM:
outRow[cell] += matrix[row + 1][cell]
if cell != LEFT:
outRow[cell] += matrix[row][cell - 1]
if cell != RIGHT:
outRow[cell] += matrix[row][cell + 1]
if row != TOP and cell != LEFT:
outRow[cell] += matrix[row - 1][cell - 1]
if row != TOP and cell != RIGHT:
outRow[cell] += matrix[row - 1][cell + 1]
if row != BOTTOM and cell != LEFT:
outRow[cell] += matrix[row + 1][cell - 1]
if row != BOTTOM and cell != RIGHT:
outRow[cell] += matrix[row + 1][cell + 1]
outMatrix.append(outRow)
return outMatrix
def arrayReplace(inputArray, elemToReplace, substitutionElem):
return [x if x != elemToReplace else substitutionElem for x in inputArray]
def evenDigitsOnly(n):
return all((True if digit in ('0', '2', '4', '6', '8') else False for digit in str(n)))
def variableName(name):
return name.replace("_", "").isalnum() and not name[0].isdigit()
def alphabeticShift(inputString):
return ''.join([chr(ord(x) + 1) if x != 'z' else 'a' for x in inputString])
def chessBoardCellColor(cell1, cell2):
color1 = ((ord(cell1[0]) - ord('A')) + ord(cell1[1]) - ord('1')) % 2 == 0
color2 = ((ord(cell2[0]) - ord('A')) + ord(cell2[1]) - ord('1')) % 2 == 0
return color1 == color2
def circleOfNumbers(n, firstNumber):
return (firstNumber + (n / 2)) % n
def depositProfit(deposit, rate, threshold):
year = 0
while deposit < threshold:
deposit *= 1 + (rate / 100)
year += 1
return year
def absoluteValuesSumMinimization(a):
sums = {}
for num in a:
total = sum([abs(a[i] - num) for i in range(len(a))])
if total in sums:
sums[total] = min(num, sums[total])
else:
sums[total] = num
print(sums)
return sums[min(sums)]
import itertools
def stringsRearrangement(inputArray):
permutations = itertools.permutations(inputArray)
for array in permutations:
if testArrangement(array):
return True
return False
def testArrangement(array):
for i in range(len(array) - 1):
if sum([a != b for a, b in zip(array[i], array[i + 1])]) != 1:
return False
return True
def extractEachKth(inputArray, k):
return [inputArray[x] for x in range(len(inputArray)) if (x + 1) % k != 0]
def firstDigit(inputString):
for char in inputString:
if char.isdigit():
return char
def differentSymbolsNaive(s):
return len(set(s))
def arrayMaxConsecutiveSum(inputArray, k):
sums = [sum(inputArray[:k])]
for i in range(1, len(inputArray) - k + 1):
sums.append(sums[i - 1] - inputArray[i - 1] + inputArray[i + k - 1])
return max(sums)
def growingPlant(upSpeed, downSpeed, desiredHeight):
height = 0
days = 1
height += upSpeed
while height < desiredHeight:
days += 1
height -= downSpeed
height += upSpeed
return days
def knapsackLight(value1, weight1, value2, weight2, maxW):
if weight1 + weight2 <= maxW:
return value1 + value2
if weight1 <= maxW and (weight2 > maxW or value1 >= value2):
return value1
if weight2 <= maxW and (weight1 > maxW or value2 >= value1):
return value2
return 0
def longestDigitsPrefix(inputString):
for char in range(len(inputString)):
if not inputString[char].isdigit():
return inputString[:char]
return inputString
def digitDegree(n):
degree = 0
while len(str(n)) > 1:
n = sum((int(digit) for digit in str(n)))
degree += 1
return degree
def bishopAndPawn(bishop, pawn):
return abs(ord(bishop[0]) - ord(pawn[0])) == abs(ord(bishop[1]) - ord(pawn[1]))
def isBeautifulString(inputString):
for letter in range(ord('a'), ord('z')):
if inputString.count(chr(letter)) < inputString.count(chr(letter + 1)):
return False
return True
def findEmailDomain(address):
return address[address.rfind('@') + 1:]
def buildPalindrome(st):
if st == st[::-1]: # Check for initial palindrome
return st
index = 0
subStr = st[index:]
while subStr != subStr[::-1]: # while substring is not a palindrome
index += 1
subStr = st[index:]
return st + st[index - 1::-1]
def electionsWinners(votes, k):
winners = 0
current_winner = max(votes)
for candidate in votes:
if k > 0 and candidate + k > current_winner:
winners += 1
if k == 0 and candidate == current_winner and votes.count(candidate) == 1:
winners += 1
return winners
def isMAC48Address(inputString):
hex_chars = ('1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
'A', 'B', 'C', 'D', 'E', 'F',
'a', 'b', 'c', 'd', 'e', 'f')
groups = inputString.split('-')
if len(groups) != 6:
return False
if not all((len(group) == 2 for group in groups)):
return False
if not all((group[0] in hex_chars and group[1] in hex_chars for group in groups)):
return False
return True
def isDigit(symbol):
return symbol.isdigit()
def lineEncoding(s):
count = 1
output = []
for char in range(1, len(s)):
if s[char] == s[char - 1]:
count += 1
else:
if count > 1:
output.append(str(count) + s[char - 1])
else:
output.append(s[char - 1])
count = 1
if s[len(s) - 1] == s[len(s) - 2]:
output.append(str(count) + s[len(s) - 1])
else:
output.append(s[len(s) - 1])
return ''.join(output)
def chessKnight(cell):
moves = 0
# Starting at the top left, going counter-clockwise
if ord(cell[0]) >= ord("b") and ord(cell[1]) <= ord("6"):
moves += 1
if ord(cell[0]) >= ord("c") and ord(cell[1]) <= ord("7"):
moves += 1
if ord(cell[0]) >= ord("c") and ord(cell[1]) >= ord("2"):
moves += 1
if ord(cell[0]) >= ord("b") and ord(cell[1]) >= ord("3"):
moves += 1
if ord(cell[0]) <= ord("g") and ord(cell[1]) >= ord("3"):
moves += 1
if ord(cell[0]) <= ord("f") and ord(cell[1]) >= ord("2"):
moves += 1
if ord(cell[0]) <= ord("f") and ord(cell[1]) <= ord("7"):
moves += 1
if ord(cell[0]) <= ord("g") and ord(cell[1]) <= ord("6"):
moves += 1
return moves
def deleteDigit(n):
num = str(n)
highest = 0
for digit in range(len(num)):
output = num[:digit] + num[digit + 1:]
if int(output) > int(highest):
highest = output
return int(highest)
def longestWord(text):
longest = []
word = []
for char in text:
if ord("A") <= ord(char) <= ord("Z") or ord("a") <= ord(char) <= ord("z"):
word.append(char)
else:
if len(word) > len(longest):
longest = word
word = []
if len(word) > len(longest):
longest = word
return "".join(longest)
def validTime(time):
groups = time.split(":")
if len(groups) != 2:
return False
if not (groups[0].isdigit() and groups[1].isdigit()):
return False
if int(groups[0]) > 23 or int(groups[1]) > 59:
return False
return True
def sumUpNumbers(inputString):
total = 0
current_num = []
for char in inputString:
if char.isdigit():
current_num.append(char)
else:
if len(current_num) > 0:
num = int("".join(current_num))
total += num
current_num = []
if len(current_num) > 0:
num = int("".join(current_num))
total += num
return total
def differentSquares(matrix):
squares = set()
for row in range(len(matrix) - 1):
for cell in range(len(matrix[row]) - 1):
square = ((matrix[row][cell], matrix[row][cell + 1]),
(matrix[row + 1][cell], matrix[row + 1][cell + 1]))
squares.add(square)
return len(squares)
def digitsProduct(product):
# New idea: add product to factors
# while max(factors) > 10: split that num into factors
if product == 0:
return 10
factors = [product]
while max(factors) > 9:
factored = findFactors(max(factors))
if factored:
factors.remove(max(factors))
factors.extend(factored)
else:
return -1
while factors.count(3) >= 2:
factors.remove(3)
factors.remove(3)
factors.append(9)
while factors.count(2) > 2:
factors.remove(2)
factors.remove(2)
factors.remove(2)
factors.append(8)
while factors.count(2) > 1:
factors.remove(2)
factors.remove(2)
factors.append(4)
while 2 in factors and 3 in factors:
factors.remove(2)
factors.remove(3)
factors.append(6)
return int("".join(map(str, sorted(factors))))
def findFactors(n):
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
return i, n // i
return False
def fileNaming(names):
outnames = []
for name in names:
if name in outnames:
k = 1
while "{}({})".format(name, k) in outnames:
k += 1
name = "{}({})".format(name, k)
outnames.append(name)
return outnames
def messageFromBinaryCode(code):
output = []
for i in range(0, len(code), 8):
letter = chr(int(code[i:i + 8], 2))
output.append(letter)
return ''.join(output)
def spiralNumbers(n):
LEFT = 'left'
RIGHT = 'right'
UP = 'up'
DOWN = 'down'
direction = RIGHT
spiral = [[0 for i in range(n)] for j in range(n)]
row = 0
cell = 0
for num in range(1, (n * n) + 1):
spiral[row][cell] = num
if direction == RIGHT:
if cell != n - 1 and spiral[row][cell + 1] == 0:
cell += 1
else:
direction = DOWN
row += 1
elif direction == DOWN:
if row != n - 1 and spiral[row + 1][cell] == 0:
row += 1
else:
direction = LEFT
cell -= 1
elif direction == LEFT:
if cell != 0 and spiral[row][cell - 1] == 0:
cell -= 1
else:
direction = UP
row -= 1
elif direction == UP:
if row != 0 and spiral[row - 1][cell] == 0:
row -= 1
else:
direction = RIGHT
cell += 1
return spiral
print(spiralNumbers(5))
def sudoku(grid):
match = [i for i in range(1, 10)]
for row in grid:
if sorted(row) != match:
return False
for column_index in range(9):
column = [grid[row_index][column_index] for row_index in range(9)]
if sorted(column) != match:
return False
for row in range(0, 9, 3):
for column in range(0, 9, 3):
box = []
box.extend(grid[row][column:column + 3])
box.extend(grid[row + 1][column:column + 3])
box.extend(grid[row + 2][column:column + 3])
if sorted(box) != match:
return False
return True
# def isAdult(age, majority):
# return age >= majority
isAdult = lambda a, m: a >= m
def bishopAndPawn(bishop, pawn):
if ord(bishop[0]) == ord(pawn[0]):
return False
else:
bishop_elm = ord(bishop[0]) + int(bishop[1])
pawn_elm = ord(pawn[0]) + int(pawn[1])
return (bishop_elm + pawn_elm) % 2 == 0
"""Below we will define an n-interesting polygon. Your task is to find the area of a polygon for a given n.
A 1-interesting polygon is just a square with a side of length 1. An n-interesting polygon is obtained by taking the
(n - 1)-interesting polygon and appending 1-interesting polygons to its rim, side by side.
You can see the 1-, 2-, 3- and 4-interesting polygons in the picture below. (PICTURE PROVIDED AT:WWW.CODESIGNAL.COM)
Example:
- For n = 2, the output should be shapeArea(n) = 5;
- For n = 3, the output should be shapeArea(n) = 13."""
def shapeArea(n):
# Case 1: If the polygon is 0-interesting, it has an area equal to zero.
if n == 0:
return None
# Case 2: If the polygon is 1-interesting, it has an area equal to one.
elif n == 1:
return 1
# Case 3: If the polygon is n-interesting, it has an area equal to the sum of the square of n
# and the square of n-1. A way that I thought of it (based on the picture provided) is the following:
# - n**2: Counted the number of the blue squares from the middle line upwards (INCLUDING the blue squares of the middle line).
# - (n-1)**2: Counted the number of the blue squares from the middle line downwards (EXCLUDING the blue squares of the middle line).
# Of course, you can easily check that the terms "upwards/downwards" could be inverted, without affecting the validity of your counting.
elif n > 1:
result = (n ** 2) + ((n - 1) ** 2)
return result
"""Ratiorg got statues of different sizes as a present from CodeMaster for his birthday, each statue having an non-negative integer size.
Since he likes to make things perfect, he wants to arrange them from smallest to largest so that each statue will be bigger than the
previous one exactly by 1. He may need some additional statues to be able to accomplish that. Help him figure out the minimum number of
additional statues needed.
Example:
For statues = [6, 2, 3, 8], the output should be makeArrayConsecutive2(statues) = 3.
Ratiorg needs statues of sizes 4, 5 and 7."""
def makeArrayConsecutive2(statues):
# Step 1: We begin by creating a new array called "stat_arr", which will accommodate the sorted version of the original "statues" array.
stat_arr = sorted(statues)
# Step 2: Furthermore, we use the first element of the sorted array as our index (that will be used in the following steps).
i = stat_arr[0]
# Step 3: We create an empty list called "result" to store our (numbered) missing statues.
result = list()
# Step 4: We initiate a while-loop with the condition that the index from Step 2 is not equal to the last (hence largest) entry of
# the stat_arr. You must make sure that you add the "incrementation by 1" part to make the while loop proceed to the next element.
while i != stat_arr[-1]:
i += 1
# Step 5: Here, using a simple if statement, we examine whether the i-th (integer) element is included in the stat_arr.
# If it is not, we append it to the result list. Otherwise, the loop continues.
if i not in stat_arr:
result.append(i)
else:
continue
# Step 6: Finally, we return the length/size of the result list, i.e. the number of our "missing" statues.
return len(result)
a, b = eval(dir()[0])
return a + b
# 28
# return sum(eval(dir()[0]), [])
# 36
# return [x for l in eval(dir()[0]) for x in l]
# def twoArraysNthElement(array1, array2, n):
# return sorted(array1 + array2)[n]
# 70
# twoArraysNthElement = lambda a, b, n: sorted(a + b)[n]
# 46
a, b, n = eval(dir()[0])
return sorted(a + b)[n]
# 40
n, d = eval(dir()[0])
while n % d < 1:
n /= d
return n
# def divideAsLongAsPossible(n, d):
# while n % d == 0:
# n /= d
# return n
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
def bstFromPreorder(self, preorder):
if not preorder:
return None
root = TreeNode(preorder[0])
i = 1
while i < len(preorder) and preorder[i] < root.val:
i += 1
root.left = self.bstFromPreorder(preorder[1:i])
root.right = self.bstFromPreorder(preorder[i:])
return root
preorder = [19, 4, 8, 11]
bst = Solution()
bst.bstFromPreorder(preorder)
def isBeautifulString(inputString):
counter = [inputString.count(i) for i in string.ascii_lowercase]
return counter[::-1] == sorted(counter)
def boxBlur(image):
def pixels(matrix, i, j):
summ = 0
for x in range(i - 1, i + 2):
for y in range(j - 1, j + 2):
summ += matrix[x][y]
mean = summ // 9
return mean
output = []
row = len(image)
col = len(image[0])
for i in range(1, row - 1):
arr = []
for j in range(1, col - 1):
arr.append(pixels(image, i, j))
output.append(arr)
return output
def chessBoardCellColor(cell1, cell2):
cell1_elm = ord(cell1[0]) + int(cell1[1])
cell2_elm = ord(cell2[0]) + int(cell2[1])
return (cell1_elm + cell2_elm) % 2 == 0
def addBorder(picture):
new_pic = []
border = ""
pic_len = len(picture)
for i in range(0, len(picture[0]) + 2):
border += "*"
new_pic.append(border)
for i in range(0, pic_len):
new_pic.append("*" + picture[i] + "*")
new_pic.append(border)
return new_pic
sulkyBoy = lambda x: not x
import itertools as i
# *re.findall('...', 'abcdefghijklmno')
# >>> 'abc', 'def', 'ghi', 'jkl', 'mno'
# [0,0,"abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"]
return [
"".join(s)
for s in i.product(
*[
"0 0 abc def ghi jkl mno pqrs tuv wxyz".split()[int(i)]
for i in eval(dir()[0])[0]
]
)
if s
]
def maxProfit(prices):
i = 0
max_profit = 0
while i < len(prices) - 1:
while i < len(prices) - 1 and prices[i] >= prices[i + 1]:
i += 1
min_pri = prices[i]
while i < len(prices) - 1 and prices[i] <= prices[i + 1]:
i += 1
max_pri = prices[i]
max_profit += max_pri - min_pri
return max_profit
print(maxProfit([1, 2, 3, 4, 5, 6]))
A, = numpy.r_[eval(dir()[0])]
A[A > 0] = sorted(A[A > 0])
return A
"""Some people are standing in a row in a park. There are trees between them which cannot be moved.
Your task is to rearrange the people by their heights in a non-descending order without moving the trees.
People can be very tall!
Example:
- For a = [-1, 150, 190, 170, -1, -1, 160, 180], the output should be sortByHeight(a) = [-1, 150, 160, 170, -1, -1, 180, 190]."""
def sortByHeight(a):
# Step 1: We begin by creating a counter, starting from 0, that will be used in the subsequent for-loop.
j = 0
# Step 2: We also create a new array, called "a_sort", where we sort (in ascending order) all elements of the given array "a"
# that are not "trees" (i.e. do not have a value of -1).
a_sort = sorted([i for i in a if i != -1])
# Step 3: By implementing a for-loop, we investigate all elements of the given array "a" (NOT a_sort!) and check:
# if the element i in array "a" is equal to -1, the for-loop continues. Otherwise, the element i in array "a" should be
# the same as element j in array "a_sort" (starting from 0 index, as defined in step 1).
# You can think of it as working through elements of array "a", disregarding the "trees" (-1s) and sorting the rest
# of the elements in ascending order (as in a_sort).
for i in range(len(a)):
if a[i] == -1:
pass
else:
a[i] = a_sort[j]
j += 1
# Step 4: The final step is the return of the modified array "a".
return a
def arrayChange(inputArray):
first = inputArray[0]
count = 0
for i in inputArray[1:]:
if i <= first:
count += first - i + 1
first = first + 1
else:
first = i
return count
"""Given two strings, find the number of common characters between them.
Example:
For s1 = "aabcc" and s2 = "adcaa", the output should be commonCharacterCount(s1, s2) = 3.
Strings have 3 common characters - 2 "a"s and 1 "c"."""
def commonCharacterCount(s1, s2):
# Step 1: We create two lists, namely s1_l and s2_l, where we store the characters of strings s1 and s2 respectively.
s1_l = list(s1)
s2_l = list(s2)
# Step 2: We also create an empty list, where we are going to store all common characters.
common = []
# Step 3: Using a for-loop, we investigate the list of the first string, element by element.
for i in s1_l:
# Step 4: If the i-th element from the list of the first string is also present in the list of the second string,
# we append it to the common array. BE CAREFUL: We must implement the s2_l.remove(i) to avoid double-counting.
# I checked myself and I can assure you that you can substitute s1_l for s2_l and vice versa (in the for-loop,
# the if statement and the double-counting term), without affecting the validity of your code.
if i in s2_l:
common.append(i)
s2_l.remove(i)
# Step 5: Finally, we return the length of the common list, to find the number of the common characters
# between the two strings given.
return len(common)
# def passwordCheck(s):
# if any(i.isdigit() for i in s) and any(i.islower() for i in s) and any(i.isupper() for i in s) and len(s) >= 5:
# return True
# else:
# return False
# passwordCheck = lambda s: (any(i.isdigit()) and any(i.islower()) and any(i.isupper())) for i in s and len(s) > 4
# Regex:
# def passwordCheck(s):
# return len(s) > 4 and all(re.search(p, s) for p in ('[A-Z]', '\d', '[a-z]'))
passwordCheck = lambda s: len(s) > 4 and all(
re.search(i, s) for i in ("[A-Z]", "\d", "[a-z]")
)
# def fractionComparison(a, b):
# d = a[0] / a[1]
# f = b[0] / b[1]
# if d < f:
# return "<"
# elif d > f:
# return ">"
# else:
# return "="
(a, b), (c, d) = eval(dir()[0])
r = (a * d) / (b * c)
return "<" if r < 1 else ">" if r > 1 else "="
# 72 chars
# Nique toi Sylvere
L, = eval(dir()[0])
s = 0
while len(L) > 1:
L = (
numpy.add(L[:-1:2], L[1::2])
if s % 2 == 0
else numpy.multiply(L[:-1:2], L[1::2])
)
s += 1
return L[0]
L, = eval(dir()[0])
s = 0
while len(L) > 1:
L = (
numpy.add(L[:-1:2], L[1::2])
if s % 2 == 0
else numpy.multiply(L[:-1:2], L[1::2])
)
s += 1
return L[0]
def addTwoDigits(n):
return (n // 10) + (n % 10)
def largestNumber(n):
return int("9" * n)
def candies(n, m):
return (m // n) * n
def seatsInTheater(nCols, nRows, col, row):
return (nCols - col + 1) * (nRows - row)
def maxMultiple(divisor, bound):
for num in range(bound, 1, -1):
if num % divisor == 0:
return num
return 0
def circleOfNumbers(n, firstNumber):
return (firstNumber + (n // 2)) % n
def lateRide(n):
hours = n // 60
minutes = n % 60
return (hours // 10) + (hours % 10) + (minutes // 10) + (minutes % 10)
def phoneCall(min1, min2_10, min11, s):
if s < min1:
return 0
if s == min1:
return 1
if s <= min1 + (min2_10 * 9):
s -= min1
return (s // min2_10) + 1
s -= min1
s -= min2_10 * 9
return (s // min11) + 10
def reachNextLevel(experience, threshold, reward):
return experience + reward >= threshold
def knapsackLight(value1, weight1, value2, weight2, maxW):
if weight1 + weight2 <= maxW:
return value1 + value2
if weight1 <= maxW and weight2 <= maxW:
return max(value1, value2)
if weight1 <= maxW:
return value1
if weight2 <= maxW:
return value2
return 0
def extraNumber(a, b, c):
if a == b:
return c
if a == c:
return b
return a
def isInfiniteProcess(a, b):
return a > b or (a % 2 != b % 2)
def arithmeticExpression(a, b, c):
return a + b == c or a - b == c or a * b == c or a / b == c
def tennisSet(score1, score2):
if max(score1, score2) == 6 and min(score1, score2) < 5:
return True
if 5 <= min(score1, score2) <= 6 and max(score1, score2) == 7:
return True
return False
def willYou(young, beautiful, loved):
return (young and beautiful) != loved
def metroCard(lastNumberOfDays):
if lastNumberOfDays == 30 or lastNumberOfDays == 28:
return [31]
return [28, 30, 31]
def killKthBit(n, k):
return n & ~(2 ** (k - 1))
def arrayPacking(a):
binary_array = [bin(num)[2:].rjust(8, '0') for num in a]
out_string = ''.join(binary_array[::-1])
return int(out_string, 2)
def rangeBitCount(a, b):
array = list(range(a, b + 1))
binary_array = [bin(num) for num in array]
count_array = [binary.count('1') for binary in binary_array]
return sum(count_array)
def mirrorBits(a):
binary = bin(a)[2:]
return int(binary[::-1], 2)
def secondRightmostZeroBit(n):
return 2 ** bin(n)[::-1].find('0', bin(n)[::-1].find('0') + 1)
def swapAdjacentBits(n):
return ((n >> 1) & 1431655765) | ((n << 1) & 2863311530)
def differentRightmostBit(n, m):
return 2 ** bin((n ^ m))[::-1].find('1')
def equalPairOfBits(n, m):
return 2 ** bin(~(n ^ m))[::-1].find('1')
def leastFactorial(n):
factorial = 1
index = 1
while factorial < n:
index += 1
factorial *= index
return factorial
def countSumOfTwoRepresentations2(n, l, r):
count = 0
a = max(n - r, l)
b = n - a
while a <= r and a <= b:
count += 1
a += 1
b -= 1
return count
def magicalWell(a, b, n):
total = 0
for i in range(n):
total += a * b
a += 1
b += 1
return total
def lineUp(commands):
count = 0
smart_student = 0
dumb_student = 0
for command in commands:
if command == 'L':
smart_student = (smart_student - 1) % 4
dumb_student = (dumb_student + 1) % 4
elif command == 'R':
smart_student = (smart_student + 1) % 4
dumb_student = (dumb_student - 1) % 4
elif command == 'A':
smart_student = (smart_student + 2) % 4
dumb_student = (dumb_student + 2) % 4
if smart_student == dumb_student:
count += 1
return count
def additionWithoutCarrying(param1, param2):
# Convert numbers to strings
str1 = str(param1)
str2 = str(param2)
# Pad both to the same length with zeroes (to the left of the numbers)
length = max(len(str2), len(str1))
str1 = str1.rjust(length, '0')
str2 = str2.rjust(length, '0')
output = []
for num1, num2 in zip(str1, str2):
result = str(int(num1) + int(num2))[-1]
output.append(result)
return int(''.join(output))
def appleBoxes(k):
red = 0
yellow = 0
for i in range(1, k + 1, 2):
yellow += i * i
for i in range(2, k + 1, 2):
red += i * i
return red - yellow
def increaseNumberRoundness(n):
string = str(n)
# Check for immediate rejection
if '0' not in string or len(string) < 2:
return False
# Since we know there's a 0, if it's not on
# the left, then we know to accept
if string[-1] != '0':
return True
# If there is only one 0, it must be at the end, so reject.
if string.count('0') == 1:
return False
# If there are any numbers between the first 0
# and the end of the string, then accept.
first_zero = string.find('0')
zero_sandwich = string[first_zero:]
return zero_sandwich.count('0') != len(zero_sandwich)
def rounders(value):
length = len(str(value))
magnitude = length - 1
for i in range(length - 1):
value = int((value / 10) + 0.5)
return value * (10 ** magnitude)
def candles(candlesNumber, makeNew):
totalBurned = 0
leftovers = 0
while candlesNumber > 0:
totalBurned += candlesNumber
leftovers += candlesNumber
candlesNumber = 0
candlesNumber = leftovers // makeNew
leftovers = leftovers % makeNew
return totalBurned
def countBlackCells(n, m):
gcd = find_gcd(n, m)
line_cells = n + m - gcd
line_corner_cells = (gcd - 1) * 2
return line_cells + line_corner_cells
def find_gcd(a, b):
while b != 0:
a, b = b, a % b
return a
def createArray(size):
return [1] * size
def arrayReplace(inputArray, elemToReplace, substitutionElem):
output = [elem if elem != elemToReplace else substitutionElem for elem in inputArray]
return output
def firstReverseTry(arr):
if len(arr) < 2:
return arr
if len(arr) < 4:
return arr[::-1]
return arr[-1:] + arr[1:-1] + arr[:1]
def concatenateArrays(a, b):
return a + b
def removeArrayPart(inputArray, l, r):
return inputArray[:l] + inputArray[r + 1:]
def isSmooth(arr):
if arr[0] != arr[-1]:
return False
if len(arr) % 2 == 0:
middle = arr[len(arr) // 2] + arr[(len(arr) // 2) - 1]
else:
middle = arr[len(arr) // 2]
return arr[0] == middle
def replaceMiddle(arr):
if len(arr) % 2 != 0:
return arr
right_middle = len(arr) // 2
middle_value = arr[right_middle] + arr[right_middle - 1]
return arr[:right_middle - 1] + [middle_value] + arr[right_middle + 1:]
def makeArrayConsecutive2(statues):
count = 0
for i in range(min(statues), max(statues)):
if i not in statues:
count += 1
return count
def isPower(n):
if n == 1:
return True
a = 2
b = 2
while a ** 2 <= n:
while a ** b <= n:
if a ** b == n:
return True
b += 1
b = 2
a += 1
return False
def isSumOfConsecutive2(n):
count = 0
right = 2
arr = [1, 2]
while right <= (n // 2) + 1:
total = sum(arr)
if total == n:
count += 1
del arr[0]
elif total < n:
right += 1
arr.append(right)
elif total > n:
del arr[0]
return count
def squareDigitsSequence(a0):
sequence = [a0]
while sequence[-1] not in sequence[:-1]:
next_value = 0
for digit in str(sequence[-1]):
next_value += int(digit) ** 2
sequence.append(next_value)
return len(sequence)
def pagesNumberingWithInk(current, numberOfDigits):
numberOfDigits -= len(str(current))
next_digits = len(str(current + 1))
while numberOfDigits >= next_digits:
current += 1
numberOfDigits -= next_digits
next_digits = len(str(current))
return current
def comfortableNumbers(l, r):
count = 0
for a in range(l, r):
for b in range(a + 1, r + 1):
a_sum = sum(int(digit) for digit in str(a))
b_sum = sum(int(digit) for digit in str(b))
if b <= a + a_sum and a >= b - b_sum:
count += 1
return count
def weakNumbers(n):
all_factors = [count_factors(num) for num in range(1, n+1)]
weaknesses = []
for num, num_factors in enumerate(all_factors, 1):
weakness = 0
for factor in all_factors[:num]:
if factor > num_factors:
weakness += 1
weaknesses.append(weakness)
weakest = max(weaknesses)
return [weakest, weaknesses.count(weakest)]
def count_factors(n):
factors = 0
for i in range(1, n+1):
if n % i == 0:
factors += 1
return factors
print(weakNumbers(500))
import math
def rectangleRotation(a, b):
n = a / (2 ** 0.5)
m = b / (2 ** 0.5)
points = (math.floor(n) * math.floor(m)) + (math.ceil(n) * math.ceil(m))
if math.floor(n) % 2 != math.floor(m) % 2:
points -= 1
return points
# rectangleRotation(6, 4)
print(rectangleRotation(8,6))
# def insertDashes(s):
# return "-".join(s).replace("- -", " ")
# 55
# insertDashes = lambda s: "-".join(s).replace("- -", " ")
# 51
# return "-".join(*eval(dir()[0])).replace("- -", " ")
# 50
# return re.sub("- -", " ", "-".join(*eval(dir()[0])))
# 49
# insertDashes = lambda s: re.sub('\B', '-', s)
# 39
return re.sub("\B", "-", *eval(dir()[0]))
# 38
def digitDegree(n):
degree = 0
while 10 <= n:
num = str(n)
n = sum(int(i) for i in num)
degree += 1
return degree
def deleteDigit(n):
num = str(n)
result = list(int("".join(num[:i] + num[1 + i :])) for i in range(len(num)))
return max(result)
def evenDigitsOnly(n):
digits_of_n = []
while n > 0:
rem = n % 10
digits_of_n.append(rem)
n = int(n / 10)
for i in range(len(digits_of_n)):
if digits_of_n[i] % 2 != 0:
return False
return True
def longestDigitsPrefix(inputString):
count = 0
for i in range(len(inputString)):
if inputString[i].isdigit():
count += 1
else:
return inputString[0:count]
return inputString
# def removeDuplicateStrings(a):
# return list(OrderedDict.fromkeys(a))
# 64
# removeDuplicateStrings = lambda a: list(OrderedDict.fromkeys(a))
# 60
return list(OrderedDict.fromkeys(*eval(dir()[0])))
# 49
def extractEachKth(inputArray, k):
inp = []
for i in range(len(inputArray)):
if (i + 1) % k == 0:
pass
else:
inp.append(inputArray[i])
return inp
"""Given an array of integers, find the pair of adjacent elements that has the largest product and return that product.
Example:
For inputArray = [3, 6, -2, -5, 7, 3], the output should be adjacentElementsProduct(inputArray) = 21.
7 and 3 produce the largest product."""
def adjacentElementsProduct(inputArray):
# Step 1: Initially, define an empty array where we will store the products of adjacent elements from the input array.
ArrayEnd = []
# Step 2: Using a for-loop, we go over all entries of the input array, calculating the products of adjacent elements
# and appending them to the empty array from step 1.
for i in range(len(inputArray) - 1):
ArrayEnd.append(inputArray[i] * inputArray[i + 1])
# Step 3: We seek the largest entry in "ArrayEnd" from step 1, using the max() function.
maximum = max(ArrayEnd)
return maximum
"""After becoming famous, the CodeBots decided to move into a new building together.
Each of the rooms has a different cost, and some of them are free, but there's a rumour that all the free rooms are haunted!
Since the CodeBots are quite superstitious, they refuse to stay in any of the free rooms, or any of the rooms below any of the free rooms.
Given matrix, a rectangular matrix of integers, where each value represents the cost of the room, your task is to return
the total sum of all rooms that are suitable for the CodeBots (ie: add up all the values that don't appear below a 0).
Example:
For
matrix = [[0, 1, 1, 2],
[0, 5, 0, 0],
[2, 0, 3, 3]]
the output should be matrixElementsSum(matrix) = 9.
example 1: There are several haunted rooms, so we'll disregard them as well as any rooms beneath them.
Thus, the answer is 1 + 5 + 1 + 2 = 9. (PICTURE PROVIDED AT:WWW.CODESIGNAL.COM)
For
matrix = [[1, 1, 1, 0],
[0, 5, 0, 1],
[2, 1, 3, 10]]
the output should be matrixElementsSum(matrix) = 9.
example 2:
Note that the free room in the final column makes the full column unsuitable for bots (not just the room directly beneath it).
Thus, the answer is 1 + 1 + 1 + 5 + 1 = 9. (PICTURE PROVIDED AT:WWW.CODESIGNAL.COM)"""
def matrixElementsSum(matrix):
# Step 1: We begin by defining the number of rows and columns inside our given matrix.
# You can conceive the number of rows as the number of nested arrays inside the main array and
# the number of columns as the number of elements in the first nested array.
# Feel free to convince yourself that this is the case by referring to the examples of matrices shown above.
rows = len(matrix)
cols = len(matrix[0])
# Step 2: Furthermore, create a new variable called "summ" (from summation) and set it equal to zero.
# It will be used in the following for-loop.
summ = 0
# Step 3: Here we have an unusual for-inside-a-for loop (compared to the one that we usually observe when dealing with matrices).
# As we are interested in the position of elements in columns (elements BELOW 0s), the outside for-loop works across all columns
# whilst the nested for-loop works across all rows.
for j in range(cols):
for i in range(rows):
# Step 4: If, while counting, the loop meets an element whose value is zero, the counting stops.
# Otherwise, it continues counting, each time adding the value of the i-th / j-th element to the "summ" variable, defined in step 2.
if matrix[i][j] == 0:
break
summ += matrix[i][j]
# Step 5: Therefore, we end up with the total sum of non-zero elements whose position in a column is not
# below an element of value zero.
return summ
def findEmailDomain(address):
address_spl = address.split("@")
c = [i for i in address_spl]
if len(address_spl) == 2:
return c[1]
if len(address_spl) == 3:
return c[2]
from itertools import groupby
def lineEncoding(s):
s2 = ""
for k, g in groupby(s):
l = len(list(g))
if l == 1:
s2 += k
else:
s2 += str(l) + k
return s2
def areEquallyStrong(yourLeft, yourRight, friendsLeft, friendsRight):
personal_max = max(yourLeft, yourRight)
friend_max = max(friendsLeft, friendsRight)
sum1 = yourLeft + yourRight
sum2 = friendsLeft + friendsRight
if sum1 == sum2 and personal_max == friend_max:
return True
return False
import requests
import pandas as pd
def productExceptSelf(nums, m, first=True):
# uses divide and conquer approach from Khan academy!
# may need to upgrade with prime factorization
# and fast modular exponents using binary!
# make map of prime factors and their exponents (number of each factor)
# by breaking down individual array items
# remove individual values from map for each item in array (reduce number of each factor present in current item)
# convert exponent to binary and get modular exponents for each factor (see here: https://www.khanacademy.org/computing/computer-science/cryptography/modarithmetic/a/fast-modular-exponentiation)
# finally, combine all different prime factors for the number and add to array
# reduce array by summing and final mod as shown
if len(nums) == 1:
return nums[0] % m
if first:
output = []
j = len(nums)
mid = (j - 1) // 2
# left_mod = productExceptSelf(nums[0:mid], m, False)
# right_mod = productExceptSelf(nums[mid:], m, False)
for i in range(j):
arr = nums[:]
arr.pop(i)
# print(arr)
# if(i<mid):
# mult_val = right_mod
# else:
# mult_val = left_mod
output.append(productExceptSelf(arr, m, False))
# print(output)
return sum(output) % m
else:
mid = len(nums) // 2
left = productExceptSelf(nums[0:mid], m, False)
right = productExceptSelf(nums[mid:], m, False)
return (left * right) % m
arr = pd.read_json("./test-16.json").loc["nums", "input"]
# print(arr)
print(productExceptSelf(arr, 9999, first=True))
# works for small cases, need to use divide and conquer according to Khan
# causes overflow issues
# #first, get the number of all nums multiplied
# largest_num = 1
# for num in nums:
# largest_num*=num
# print(largest_num)
# #get the array of modulo m elements
# fg_arr = [(largest_num/num) % m for num in nums]
# print(fg_arr)
# total = sum(fg_arr) % m
# return total
# 52
# def triangleExistence(t):
# t.sort()
# return t[0] + t[1] > t[2]
# 46
# triangleExistence = lambda t: sum(t) - max(t) > max(t)
# 45
# t, = eval(dir()[0])
#
# t.sort()
# return t[0] + t[1] > t[2]
# 43
# t, = eval(dir()[0])
# return sum(t) - max(t) > max(t)
# 40
a, b, c = sorted(*eval(dir()[0]))
return a + b > c
def messageFromBinaryCode(code):
phrase = ""
bits = [int(code[i * 8 : i * 8 + 8], 2) for i in range(len(code) // 8)]
for j in range(len(bits)):
phrase += chr(bits[j])
return phrase
"""Given a year, return the century it is in. The first century spans from the year 1 up to and including the year 100, the second -
from the year 101 up to and including the year 200, etc.
Example:
- For year = 1905, the output should be centuryFromYear(year) = 20;
- For year = 1700, the output should be centuryFromYear(year) = 17. """
def centuryFromYear(year):
# We begin by getting the INTEGER quotient of the division of the year given by 100.
# This will give us the first two digits, which would be the century.
cen = int(year / 100)
# However, we should keep in mind that we refer to years between e.g. 1701 - 1800
# as the "18th century". Hence, we implement a while loop, where the condition is
# that the year is a positive integer (which is always true). If the remainder of the
# division of the year by 100 is 0, then the two first digits of the division represent
# the century. Otherwise, if the remainder is non-zero, the century is found by adding 1
# to the result of the division (i.e. "cen").
while year >= 1:
if year % 100 == 0:
return year / 100
else:
return cen + 1
def isIPv4Address(inputString):
str_split = inputString.split(".")
count = 0
if len(str_split) != 4:
return False
for i in range(0, 4):
if str_split[i] == "" or str_split[i] == "00" or str_split[i] == "01":
return False
if re.search("[a-zA-Z]", str_split[i]):
count += 1
if count > 0:
return False
if str_split[i].isnumeric():
if int(str_split[i]) < 0:
return False
if int(str_split[i]) > 255:
return False
return True
"""Write a function that reverses characters in (possibly nested) parentheses in the input string.
Input strings will always be well-formed with matching ()s.
Example:
- For inputString = "(bar)", the output should be reverseInParentheses(inputString) = "rab";
- For inputString = "foo(bar)baz", the output should be reverseInParentheses(inputString) = "foorabbaz";
- For inputString = "foo(bar)baz(blim)", the output should be reverseInParentheses(inputString) = "foorabbazmilb";
- For inputString = "foo(bar(baz))blim", the output should be reverseInParentheses(inputString) = "foobazrabblim".
Because "foo(bar(baz))blim" becomes "foo(barzab)blim" and then "foobazrabblim"."""
def reverseInParentheses(inputString):
# Step 1: We create a for-loop that goes over all elements of the input string. If element i is an opening bracket, then i
# is defined as "start". In a similar manner, if element i is a closing bracket, i is defined as "end". NOTE THAT
# if you write it as "i = start" or "i = end", an error will pop up (tested) as you would have not defined any variables
# under those names, whilst the way that is written now you define as "start" and "end" elements that are
# "(" and ")" respectively.
for i in range(len(inputString)):
if inputString[i] == "(":
start = i
if inputString[i] == ")":
end = i
# Step 2: Furthermore, we apply the function inside itself and concatenate the individual modified parts:
# the part of the input string up to (and NOT including) the "starting" point (i.e. opening bracket) is left intact.
# The same goes for the part of the input string one element AFTER the "ending" point (i.e. closing bracket)
# till the actual end of the input string. The part of the input string that is located one element after
# the starting point ("start"+1 included) and up to the "ending" point NOT included is reversed.
return reverseInParentheses(
inputString[:start]
+ inputString[start + 1 : end][::-1]
+ inputString[end + 1 :]
)
# Step 3: To conclude, we return the modified input string.
return inputString
"""Given a sequence of integers as an array, determine whether it is possible to obtain a strictly increasing sequence
by removing no more than one element from the array.
Note: sequence a0, a1, ..., an is considered to be a strictly increasing if a0 < a1 < ... < an.
Sequence containing only one element is also considered to be strictly increasing.
Example:
- For sequence = [1, 3, 2, 1], the output should be almostIncreasingSequence(sequence) = false.
There is no one element in this array that can be removed in order to get a strictly increasing sequence.
- For sequence = [1, 3, 2], the output should be almostIncreasingSequence(sequence) = true.
You can remove 3 from the array to get the strictly increasing sequence [1, 2].
Alternately, you can remove 2 to get the strictly increasing sequence [1, 3]."""
def almostIncreasingSequence(sequence):
# Step 1: We begin by assigning the length of the given sequence to the variable n.
n = len(sequence)
# Step 2: By definition, if the sequence contains up to 1 elements, it is considered to be strictly increasing.
if n <= 2:
return True
# Step 3: We set up two counters, namely c1 and c2, so that we count how many elements should be removed.
# NOTE THAT c1 refers ONLY to adjacent elements whilst c2 refers to elements just before and just after the i-th element.
c1 = 0
c2 = 0
# Step 4: This for-loop (and its content) is a tricky part. The range of the for-loop starts from 1 and goes all the way to (n-1)th element.
# BE CAREFUL: We set n-1 to avoid getting our index out of range in the second if statement inside the for-loop.
for i in range(1, n - 1):
# Step 5: If the element prior to the index element i has a bigger value than i, we add 1 hit to the first counter.
if sequence[i - 1] >= sequence[i]:
c1 += 1
# Step 6: If the element just before the index element i has a bigger value than the element just after i,
# we add 1 hit to the second counter.
if sequence[i - 1] >= sequence[i + 1]:
c2 += 1
# Step 7: If the element two places to the left of the index element i has a bigger value than the element prior to i,
# we add 1 hit to the first counter.
if sequence[n - 1] <= sequence[n - 2]:
c1 += 1
# Step 8: If BOTH of the counters have up to 1 hit (that means 0 or 1 EACH), then the sequence is almost increasing.
if c1 <= 1 and c2 <= 1:
return True
else:
return False
import itertools as t
def chessKnight(cell):
knight_dir = list(t.permutations([1, 2, -1, -2], 2))
knight_dir1 = []
valid_moves = 0
for i in range(len(knight_dir)):
if sum(knight_dir[i]) != 0:
knight_dir1.append(knight_dir[i])
for x, y in knight_dir1:
if (97 <= ord(cell[0]) + x <= 104) and (1 <= int(cell[1]) + y <= 8):
valid_moves += 1
return valid_moves
def knapsackLight(value1, weight1, value2, weight2, maxW):
if weight1 > maxW and weight2 > maxW and weight1 + weight2 > maxW:
return 0
if weight1 + weight2 <= maxW:
return value1 + value2
if value1 < value2:
if weight2 > maxW:
return value1
else:
return value2
if value2 < value1:
if weight1 > maxW:
return value2
else:
return value1
if value1 == value2:
if weight1 > maxW:
return value2
else:
return value1
"""
Given a non-empty, singly linked list with head node head, return a middle node of linked list.
If there are two middle nodes, return the second middle node.
"""
# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def middleNode(self, head: ListNode) -> ListNode:
get_len = 0
current = head
while current:
get_len += 1
current = current.next
mid = get_len // 2
current = head
while mid > 0:
current = current.next
mid -= 1
return current
"""Given an array of strings, return another array containing all of its longest strings.
Example:
For inputArray = ["aba", "aa", "ad", "vcd", "aba"], the output should be allLongestStrings(inputArray) = ["aba", "vcd", "aba"]."""
def allLongestStrings(inputArray):
# Step 1: We begin by defining an empty array called "max_arr", where we will store the longest strings from the given array.
max_arr = []
# Step 2: The next step is to define the maximum string length inside our given array.
# BE CAREFUL: Variable max_len should be defined as follows. If we break it into its components, we can see that:
# max(inputArray, key = len) locates ONLY ONE of the strings that satisfies the maximum value in terms of the key parameter
# provided (which, in this case, is the string's length) and the outside len() function defines the value of this maximum length.
# You are free to test it on a random input array containing various random strings, using a Python compiler online.
max_len = len(max(inputArray, key=len))
# Step 3: Now, we go over all strings inside the input array checking if their individual length is equal to the
# maximum length value defined in step 2. If it is, then we append the respective string to the "max_arr" defined above.
for i in inputArray:
if len(i) == max_len:
max_arr.append(i)
# Step 4: We conclude by returning the max_arr.
return max_arr
"""Ticket numbers usually consist of an even number of digits. A ticket number is considered lucky
if the sum of the first half of the digits is equal to the sum of the second half.
Given a ticket number n, determine if it's lucky or not.
Example
- For n = 1230, the output should be isLucky(n) = true;
- For n = 239017, the output should be isLucky(n) = false."""
def isLucky(n):
# Step 1: We begin by creating an empty array, called "digits_of_n",
# where we will store the digits of the given number n, as individual elements.
digits_of_n = []
# Step 2: We also create a new variable, called "summ" (from summation), and set its value to zero.
# It will be useful in one of the later steps.
summ = 0
# Step 3: I have personally seen this while-loop trick being used to split numbers into their individual digits.
# As it will take quite a long text to explain this comprehensively, I'd suggest you use the print() function in each
# step to see what how each of these steps works. The important thing to mention is the "appending" step where each
# digit, where each digit is stored as an elememnt in "digits_of_n" array.
while n > 0:
rem = n % 10
digits_of_n.append(rem)
n = int(n / 10)
# Step 4: Furthermore, we implement a for-loop that goes over all elements inside the "digits_of_n" array, one by one.
# If the element's index is up to the middle of the length of the array, we add the element's numeric value to "summ".
# Otherwise, we subtract it. NOTE THAT for arrays of even length, you can find the "middle" by dividing the length by 2
# (i.e. for arrays of length 4, the 2nd element is the "middle), whilst for array of odd length, the "middle" is the
# element having equal numbers of elements on both its sides.
for i in range(len(digits_of_n)):
if i < len(digits_of_n) / 2:
summ += digits_of_n[i]
else:
summ -= digits_of_n[i]
# Step 5: Finally, we check if the summation is zero or not. If the sum is zero, then the ticket number is lucky,
# according to the definition of the exercise.
if summ == 0:
return True
return False
def isMAC48Address(inputString):
str_split = inputString.split("-")
count = 0
if len(inputString) != 17:
return False
if len(str_split) != 6:
return False
for i in range(0, 6):
if str_split[i] == "":
return False
if re.search("[a-zG-Z]", str_split[i]):
count += 1
if count > 0:
return False
return True
def arrayMaxConsecutiveSum(inputArray, k):
arr = [sum(inputArray[:k])]
for i in range(1, len(inputArray) - (k - 1)):
arr.append(arr[i - 1] - inputArray[i - 1] + inputArray[i + k - 1])
sort_arr = sorted(arr)
return sort_arr[-1]
def arrayMaximalAdjacentDifference(inputArray):
return max(
(abs(inputArray[i + 1] - inputArray[i]) for i in range(0, len(inputArray) - 1))
)
p, s = eval(dir()[0])
r = -1
l = len(p)
for i in range(l):
for j in range(l):
c = 0
d = p[j] - p[i]
f = s[i] - s[j]
if d * f < 1:
continue
for k in range(l):
if p[k] * f + s[k] * d == p[i] * f + s[i] * d:
c += 1
if c > r:
r = c
return r
# def smallestMultiple(l, r):
# for i in range(1,8**7):
# s = True
# for j in range(l, r+1):
# if i%j!=0:
# s = False
# if s:
# return i
def smallestMultiple(l, r):
for i in range(1, 16):
for j in range(l, r + 1):
while True:
if i % j != 0:
break
return i
def variableName(name):
str_name = [i for i in str(name)]
non_acc_chars = [
" ",
">",
"<",
":",
"-",
"|",
".",
",",
"!",
"[",
"]",
"'",
"/",
"@",
"#",
"&",
"%",
"?",
"*",
]
if str_name[0] in str([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]):
return False
for j in range(len(str_name)):
if str_name[j] in non_acc_chars:
return False
return True
def fileNaming(names):
if names == []:
return []
new_names = []
for name in names:
if name not in new_names:
new_names.append(name)
else:
for i in range(1, 1000):
new_name = name + "(" + str(i) + ")"
if new_name not in new_names:
new_names.append(new_name)
break
return new_names
from collections import Counter
def singleNumber(nums):
"""
given a list of integer with every element appears twice and a single number appears once, return the value of the
single number
"""
count = Counter(nums)
for k, v in count.items():
if v == 1:
return k
print(singleNumber([2, 2, 4, 1, 5]))
memo = {}
def isHappy(n):
"""
Is happy takes in a number and returns True if it is a happy number, False otherwise. A happy number
is a number defined by the following process: Starting with any positive integer, replace the number by the
sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay),
or it loops endlessly in a cycle which does not include 1.
"""
seen = {n: 1}
while True:
new_sq = sum([int(d) ** 2 for d in str(n)])
if n == 1:
return True
if new_sq in seen:
return False
else:
n = new_sq
seen[n] = 1
print(isHappy(19))
def spiralNumbers(n):
dims = n
elem = 1
matrix = [[0] * n for x in range(n)]
while 0 < dims:
i = n - dims
# you can sub i = n - dims ONLY in the first 2 parts
# where n - dims is in the starting parameter of the range
for j in range(n - dims, dims):
matrix[i][j] = elem
elem += 1
for i in range(n - dims + 1, dims):
matrix[i][j] = elem
elem += 1
for j in range(dims - 2, n - dims - 1, -1):
matrix[i][j] = elem
elem += 1
for i in range(dims - 2, n - dims, -1):
matrix[i][j] = elem
elem += 1
dims -= 1
return matrix
def avoidObstacles(inputArray):
for i in range(2, max(inputArray) + 2):
if i not in inputArray and all(j % i != 0 for j in inputArray):
return i
def circleOfNumbers(n, firstNumber):
return ((n / 2) + firstNumber) % n
def sumOfSquares(n):
return sum([x ** 2 for x in range(1, n + 1)])
# def sumOfTheAngles(n):
# return (n - 2) * 180
sumOfTheAngles = lambda n: (n - 2) * 180
# n = eval(dir()[0])
# return (n - 2) * 180
def sumOfTwo(a, b, v):
b = set(b)
return any(v - i in b for i in a)
def visitsOnCircularRoad(n, v):
c = 1
t = 0
for i in v:
t += min(abs(i - c), abs(n - abs(i - c)))
c = i
return t
# v = visitsOrder
# n = number of houses
# c = Current position
# t = Time
def buildPalindrome(st):
for i in range(len(st)):
sub = st[i : len(st)]
if sub[::-1] == sub:
missing = st[0:i]
return st + missing[::-1]
return st
checkPalindrome = lambda x: x == x[::-1]
# greetPerson = lambda n: "Hello, " + n
greetPerson = "Hello, {}".format
def growingPlant(upSpeed, downSpeed, desiredHeight):
day_count = 0
height = 0
while height <= desiredHeight:
height = height + upSpeed
day_count += 1
if height < desiredHeight:
height = height - downSpeed
else:
return day_count
def twoPointerSum(nums, target):
"""
Given a sorted array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
"""
l = 0
r = len(nums) - 1
while l < r:
if nums[l] + nums[r] == target:
return [l, r]
elif nums[l] + nums[r] > target:
r -= 1
else:
l += 1
nums = [5, 25, 75]
target = 100
print(twoPointerSum(nums, target))
# def gasPrediction(driveDistances, currentGasLevel, avgMileage):
# a = sum(driveDistances) / 12 / avgMileage
# print(a)
# if a > currentGasLevel:
# return True
# else:
# return False
# d, c, a = eval(dir()[0])
# return sum(d) / 12 / a > c
# 39 chars
gasPrediction = lambda d, c, a: sum(d) / 12 / a > c
def digitsProduct(product):
if product == 0:
return 10
if product == 1:
return 1
for i in range(0, 4000):
p = 1
for j in str(i):
p *= int(j)
if p == product:
return i
return -1
def depositProfit(deposit, rate, threshold):
i = 0
while deposit < threshold:
deposit += deposit * rate * 0.01
i += 1
return i
def depositProfit(deposit, rate, threshold):
year_count = 0
while deposit < threshold:
deposit = deposit + (deposit * (rate / 100))
year_count += 1
return year_count
from itertools import permutations as p
def stringsRearrangement(inputArray):
p_list = list(p(inputArray))
for i in range(len(p_list)):
count1 = 0
for j in range(len(p_list[0]) - 1):
count2 = 0
for k in range(len(p_list[0][0])):
if p_list[i][j][k] != p_list[i][j + 1][k]:
count2 += 1
if count2 == 1:
count1 += 1
if count1 >= (len(p_list[0])) - 1:
return True
return False
def palindromeRearranging(inputString):
odd_count = 0
char_set = set(inputString)
for i in char_set:
char_count = inputString.count(i)
if char_count % 2 != 0:
odd_count += 1
if odd_count <= 1:
return True
return False
# def fractionReducing(f):
# return [i / math.gcd(f[0], f[1]) for i in f]
# fractionReducing = lambda f: [i / math.gcd(f[0], f[1]) for i in f]
f, = eval(dir()[0])
return [i / math.gcd(f[0], f[1]) for i in f]
def arrayReplace(inputArray, elemToReplace, substitutionElem):
new = []
for i in range(len(inputArray)):
if inputArray[i] == elemToReplace:
new.append(substitutionElem)
else:
new.append(inputArray[i])
return new
def alphabeticShift(inputString):
return "".join(chr(ord(i) + 1) if i != "z" else "a" for i in inputString)
def areSimilar(a, b):
check_a = []
check_b = []
count = 0
for i in range(len(a)):
if a[i] != b[i]:
count += 1
check_a.append(a[i])
check_b.append(b[i])
if count == 0:
return True
elif count == 2:
return set(check_a) == set(check_b)
else:
return False
def differentSquares(matrix):
rows = len(matrix)
cols = len(matrix[0])
sq_arr = []
sq_count = 0
for i in range(rows - 1):
for j in range(cols - 1):
sq_2x2 = [
matrix[i][j],
matrix[i][j + 1],
matrix[i + 1][j],
matrix[i + 1][j + 1],
]
if sq_2x2 not in sq_arr:
sq_arr.append(sq_2x2)
sq_count += 1
return sq_count
def maxSubarray(A):
# A: inputArray
# m: Max
#
#
m = e = 0
for i in A:
e += i
if e < 0:
e = 0
if m < e:
m = e
return m
l, s = eval(dir()[0])
r = []
c = 1
t = [[int(s[i : i + 2]) for i in [1, 4]] + [s[7:9]] for s in l]
t = [f"{x // 60:02d}:{x % 60:02d}:{y:02d},{z.ljust(3, str(0))}" for x, y, z in t] + [
s + ",000"
] # if I replace str(0) with '0', this increases to 359 characters wtf
for a, b in enumerate(l):
r.extend([str(c), t[a] + " --> " + t[a + 1], b[11:], ""])
c += 1
return r[:-1]
# def mySubstring(s, l, r):
# return s[l:r+1]
mySubstring = lambda s, l, r: s[l : r + 1]
def isSum(value):
s = 0
for i in range(100):
s += i
if s == value:
return True
# def countSumOfTwoRepresentations3(n, l, r):
# if r < n // 2 or l > n // 2:
# return 0
# return n // 2 - max(l, n-r) + 1
# 87
# countSumOfTwoRepresentations3 = lambda n, l, r: max(n // 2 - max(l, n-r) + 1, 0)
# 66
n, l, r = eval(dir()[0])
return max(n // 2 - max(l, n - r) + 1, 0)
# 50