<jupyter_start><jupyter_text>**TUDev's Solving Coding Challenges with Python!** **Coding challenges are great not only to hone your computer science skills, but to develop your problem solving skills in general. In many of these challenges, there are multiples ways to solve the problems. These solutions also have varying degrees of efficiency, some will be faster while other will be more memory efficient. You will run into these tradeoffs in industry and academia, which is why it's great to get started now!** **In this workshop we will tackle problems from LeetCode, HackerRank and Project Euler. Let's get started with a relatively easy problem to warm up!**<jupyter_code>#timeit measures the execution time of python code
#memory_profiler measures the memory used for python code

!pip install memory_profiler
%load_ext memory_profiler<jupyter_output>Collecting memory_profiler
  Downloading https://files.pythonhosted.org/packages/8f/fd/d92b3295657f8837e0177e7b48b32d6651436f0293af42b76d134c3bb489/memory_profiler-0.58.0.tar.gz
Requirement already satisfied: psutil in /usr/local/lib/python3.6/dist-packages (from memory_profiler) (5.4.8)
Building wheels for collected packages: memory-profiler
  Building wheel for memory-profiler (setup.py) ... [?25l[?25hdone
  Created wheel for memory-profiler: filename=memory_profiler-0.58.0-cp36-none-any.whl size=30181 sha256=1a00c7d35780945b37875ef13b1c59c77b5608c3791a960a9c6a702722890648
  Stored in directory: /root/.cache/pip/wheels/02/e4/0b/aaab481fc5dd2a4ea59e78bc7231bb6aae7635ca7ee79f8ae5
Successfully built memory-profiler
Installing collected packages: memory-profiler
Successfully installed memory-profiler-0.58.0<jupyter_text>[**HackerRank - If-Else Challenge**](https://www.hackerrank.com/challenges/py-if-else/problem) **Task** Given an integer, $n$ , perform the following conditional actions:*   If $n$ is odd, print **Weird***   If $n$ is even and in the inclusive range of **2** to **5**, print **Not Weird***   If $n$ is even and in the inclusive range of **6** to **20**, print **Weird***   If $n$ is even and greater than **20**, print **Not Weird** **Input Format**A single line containing a positive integer, $n$. **Constraints**$1 \leq n \leq 100$ **Output Format**Print **Weird** if the number is weird. Otherwise, print **Not Weird**.<jupyter_code>#the first iteration of our code
#%%timeit
#%%memit
#this will take the input
if __name__ == '__main__':
    n = int(input().strip())

    #our solution
#second iteration
#%%timeit
#%%memit
if __name__ == '__main__':
    n = int(input().strip())

    #our solution<jupyter_output>5
Weird<jupyter_text>Great! Let's try a LeetCode challenge! [**LeetCode - Number of Steps to Reduce a Number to Zero**](https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/) **Task**Given a non-negative integer num, return the number of steps to reduce it to zero. If the current number is even, you have to divide it by 2, otherwise, you have to subtract 1 from it. **Example 1**:**Input:** num = 14**Output:** 6**Explanation:**Step 1) 14 is even; divide by 2 and obtain 7.Step 2) 7 is odd; subtract 1 and obtain 6.Step 3) 6 is even; divide by 2 and obtain 3.Step 4) 3 is odd; subtract 1 and obtain 2.Step 5) 2 is even; divide by 2 and obtain 1. Step 6) 1 is odd; subtract 1 and obtain 0. **Example 2**:**Input:** num = 8**Output:** 4 **Example 3**:**Input:** num = 123**Output:** 12 **Constraints:***   $0 \leq num \leq 10^6$<jupyter_code>#first iteration

def numberOfSteps(num):


#check to see if our function answers the example questions correctly

#print(numberOfSteps(14),numberOfSteps(8), numberOfSteps(123))

#%timeit numberOfSteps(50)
#%memit numberOfSteps(50)
#the first iteration had unnecessary if-statement, after we drop those our code runs faster

def numberOfSteps(num):



#print(numberOfSteps(14),numberOfSteps(8), numberOfSteps(123))

#%timeit numberOfSteps(50)
#%memit numberOfSteps(50)<jupyter_output>6 4 12<jupyter_text>**[LeetCode - Palindrome Number](https://leetcode.com/submissions/detail/370682551/)** **Task**Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.Follow up: Could you solve it without converting the integer to a string? **Example 1**:**Input:** x = 121**Output:** True **Example 2**:**Input:** x = -121**Output:** False**Explanation:** From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. **Example 3**:**Input:** x = 10**Output:** False**Explanation:** Reads 01 from right to left. Therefore it is not a palindrome. **Example 4**:**Input:** x = -101**Output:** False **Constraints***   $-2^{31} \leq x \leq 2^{31}-1$<jupyter_code>def isPalindrome(x):


#print(isPalindrome(121))
#print(isPalindrome(-121))
#print(isPalindrome(10))
#print(isPalindrome(-101))

#%timeit isPalindrome(121)
#%memit isPalindrome(121)<jupyter_output>True
False
False
False
peak memory: 157.80 MiB, increment: 0.00 MiB<jupyter_text>**[LeetCode - Create Target Array in the Given Order](https://leetcode.com/problems/create-target-array-in-the-given-order/)** **Task**Given two arrays of integers nums and index. Your task is to create a *target* array under the following rules:*   Initially target array is empty.*   From left to right read nums[i] and index[i], insert at index index[i] the value nums[i] in target array.*   Repeat the previous step until there are no elements to read in nums and index.Return the target array.It is guaranteed that the insertion operations will be valid. **Example 1**:**Input:** nums = [0,1,2,3,4], index = [0,1,2,2,1]**Output:** [0,4,1,3,2]**Explanation:**| nums  | index  | target  ||---|---|---|| 0  |  0 | [0]  || 1  |  1 | [0,1]  || 2  |   2| [0,1,2]  ||  3 |  2 | [0,1,3,2]  ||  4 |  1 | [0,4,1,3,2]  | **Example 2**:**Input:** nums = [1,2,3,4,0], index = [0,1,2,3,0]**Output:** [0,1,2,3,4]**Explanation:**| nums  | index  | target  ||---|---|---|| 1  |  0 | [1]  || 2  |  1 | [1,2]  || 3  |   2| [1,2,3]  ||  4 |  3 | [1,2,3,4]  ||  0 |  0 | [0,1,2,3,4]  | **Example 3**:**Input:** nums = [1], index = [0]**Output:** [1] **Constraints**:*   1 <= nums.length, index.length <= 100*   nums.length == index.length*   0 <= nums[i] <= 100*   0 <= index[i] <= i<jupyter_code>def createTargetArray(nums, index):


#print(createTargetArray([0,1,2,3,4], [0,1,2,2,1]))
#print(createTargetArray([1,2,3,4,0], [0,1,2,3,0]))
#print(createTargetArray([1], [0]))

#%timeit createTargetArray([0,1,2,3,4], [0,1,2,2,1])
#%memit createTargetArray([0,1,2,3,4], [0,1,2,2,1])<jupyter_output>[0, 4, 1, 3, 2]
[0, 1, 2, 3, 4]
[1]<jupyter_text>**Finally let's try a question from Project Euler** **[Project Euler - Multiples of 3 and 5](https://projecteuler.net/problem=1)** **Task**If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.Find the sum of all the multiples of 3 or 5 below 1000.<jupyter_code><jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15151515151515152, 'fraction_numerical': 0.06632361349342482, 'mean_word_length': 5.110917030567686, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 20, 'https://': 6, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '297891', 'license': 'MIT', 'n_tokens_mistral': 2665, 'n_tokens_neox': 2403, 'n_words': 796, 'path': "Solving Programming Problems with Python Workshop/TUDev's_Solving_Coding_Challenges_with_Python!_BLANK.ipynb", 'repo_name': 'tudev/Workshops-2020-2021', 'size': '6996'}