Coding Contract Challenges - (Work in Progress)

Coding Contract Challenges - (Work in Progress)

Algorithmic Stock Trader II - Complete


Coding Contract Challenges - (Work in Progress) image 1

Algorithmic Stock Trader II You are attempting to solve a Coding Contract. You have 10 tries remaining, after which the contract will self-destruct. You are given the following array of stock prices (which are numbers) where the i-th element represents the stock price on day i: 9,151,169,35,194,68,19,148,29,97,12,29 Determine the maximum possible profit you can earn using as many transactions as you'd like. A transaction is defined as buying, and then selling one share of the stock. Note that you cannot engage in multiple transactions at once. In other words, you must sell the stock before you buy it again. If no profit can be made, then the answer should be 0

Question Summary:

1. The last line of the question suggest the answer should be given in terms of a cumulative unit value. The rest of the description is a Red Herring.

2. [i-th] element pertains to the index of the array, using the power of hindsight it is possible to compare the index of array iterations to see if you should have purchased or held your money based on stock value.

Given the above assertions we derive the following answer using Python.

import numpy as np # Build the question into a numpy array stock_prices = np.array([9, 151, 169, 35, 194, 68, 19, 148, 29, 97, 12, 29]) # Create a placeholder variable to store profits profit = 0 # Find the length of the array so we can derive logic from the index length = len(stock_prices) # Use a for loop to enumerate the array for i, elem in enumerate(stock_prices): # We want to exclude Iterating to the last day of the stock price array as we don't have a future price to compare if i < (length - 1): # We want to compare future stock price to the current stock price and store in a variable diff = stock_prices[i+1] - stock_prices

# If the compared price is greater than zero it was worth purchasing

if diff > 0:

# We add the positive theoretical purchase to our profit placeholder

profit = profit + diff

# We print out the final value of our theoretical profit to get the answer

print(profit)

Generate IP Addresses - Complete


Coding Contract Challenges - (Work in Progress) image 15

Coding Contract Challenges - (Work in Progress) image 16
Coding Contract Challenges - (Work in Progress) image 17

Generate IP Addresses You are attempting to solve a Coding Contract. You have 10 tries remaining, after which the contract will self-destruct. Given the following string containing only digits, return an array with all possible valid IP address combinations that can be created from the string: 168148150114 Note that an octet cannot begin with a '0' unless the number itself is actually 0. For example, '192.168.010.1' is not a valid IP. Examples: 25525511135 -> [255.255.11.135, 255.255.111.35] 1938718066 -> [193.87.180.66]

Question Summary:

1. On first take I thought this was a permutation type problem, however the scope is much smaller as it does not intend for you to rearrange any of the number combinations from the digit string, only shift the decimal places around to see how many valid addresses you could make.

Given the above assertions we derive the following answer using Python.

# Function checks whether IP digits are valid def is_valid(ip): # Splitting by "." ip = ip.split(".") # Checking for the corner cases for i in ip: # We don't want 4 number octets # We also want to make sure the octet arrangement is between 0 - 255 if (len(i) > 3 or int(i) < 0 or int(i) > 255): return False # We Don't want our Octet consisting only of 0 if len(i) > 1 and int(i) == 0: return False # We don't want our Octet to lead with a 0 if (len(i) > 1 and int(i) != 0 and i[0] == '0'): return False # If we didnt catch on a corner case they our IP address must be all good return True # Function converts string to IP address def convert(s): sz = len(s) # Check for string size if sz > 12: return [] snew = s # This varible is where we will store any valid answers found in the below loop ip_list = [] # Generating different combinations. for i in range(1, sz - 2): for j in range(i + 1, sz - 1): for k in range(j + 1, sz): snew = snew[:k] + "." + snew[k:] snew = snew[:j] + "." + snew[j:] snew = snew[:i] + "." + snew[i:] # Check for the validity of combination if is_valid(snew): ip_list.append(snew) snew = s return ip_list # Variable to hold the digit string provided by the question digits: str = "168148150114" # Customising the print output to drop the string quotations for the expected answer format print('[%s]' % ', '.join(map(str, convert(digits))))

Unique Paths In A Grid I - Complete


Coding Contract Challenges - (Work in Progress) image 24
Coding Contract Challenges - (Work in Progress) image 25
Coding Contract Challenges - (Work in Progress) image 26

Unique Paths in a Grid I You are attempting to solve a Coding Contract. You have 10 tries remaining, after which the contract will self-destruct. You are in a grid with 10 rows and 3 columns, and you are positioned in the top-left corner of that grid. You are trying to reach the bottom-right corner of the grid, but you can only move down or right on each step. Determine how many unique paths there are from start to finish. NOTE: The data returned for this contract is an array with the number of rows and columns: [10, 3]

Question Summary:

1. This is a common programming challenge in computer science and can be answered in a range of create ways. You will find the rules already follow the natural order of loop based iterations (Moving Right across an array, Moving Down after Finishing a Line of Code). This logic does not need to be programmed into your function.

2. The only real challenge here is coming up with a solution (A tracking method) on how many unique paths you can take to get from array cell [1][1] or [0][0] if you prefer, to cell [10][3]. Most solutions tend to leave a trace on where they have been previously by setting a value to each cell as they iterate through.

Given the above assertions we derive the following answer using Python.

# Create a Function which accepts the dimension variables from the Question def numberOfPaths(m, n): if m == 1 or n == 1: return 1 return numberOfPaths(m - 1, n) + numberOfPaths(m, n - 1) # Import Grid Dimensions from the Question m: int = 10 n: int = 3 # Print the result of our Function print(numberOfPaths(m, n))

Unique Paths In A Grid II - Work In Progress


Coding Contract Challenges - (Work in Progress) image 34
Coding Contract Challenges - (Work in Progress) image 35

## Sorry I accidentally Augmented my save before finishing this coding challenge, I am yet to find it again so I can continue working on this problem. ###

You are attempting to solve a Coding Contract. You have 10 tries remaining, after which the contract will self-destruct. You are located in the top-left corner of the following grid: 0,1,0,0,1,1,0, 0,0,1,0,0,0,0, 0,0,1,1,0,0,0, 0,1,0,0,0,0,0, 0,0,0,0,0,0,0, 0,0,0,0,0,0,1, 0,1,0,0,0,0,0, 0,0,0,0,0,0,1, 0,0,1,0,1,0,0, You are trying reach the bottom-right corner of the grid, but you can only move down or right on each step. Furthermore, there are obstacles on the grid that you cannot move onto. These obstacles are denoted by '1', while empty spaces are denoted by 0. Determine how many unique paths there are from start to finish. NOTE: The data returned for this contract is an 2D array of numbers representing the grid.

Summary of the Question:

# Unique Rules:

# 1. You are located in the top-left corner of the following 2D array [0][0]

# 2. You must reach the bottom-right corner of the array [8][6]

# 3. Can only move down, or right with each step (v or >)

# 4. Can only move into locations denoted by 0

# Question: How many unique paths are there from start to finish?

Using Python this 2 dimensional matrix can be presented as a Numpy array, and dressed up to look pretty in matplotlib with the following code:

import numpy as np import matplotlib.pyplot as plt matrix = np.array([[0, 1, 0, 0, 1, 1, 0, ], [0, 0, 1, 0, 0, 0, 0, ], [0, 0, 1, 1, 0, 0, 0, ], [0, 1, 0, 0, 0, 0, 0, ], [0, 0, 0, 0, 0, 0, 0, ], [0, 0, 0, 0, 0, 0, 1, ], [0, 1, 0, 0, 0, 0, 0, ], [0, 0, 0, 0, 0, 0, 1, ], [0, 0, 1, 0, 1, 0, 0, ]]) plt.matshow(matrix, fignum="Unique Paths in a Grid II") plt.plot(0, 0, '*', color='g', label='Start') # Start Point (As X, Y) plt.plot(6, 8, '*', color='r', label='Finish') # Finish Point (As X, Y) plt.legend() plt.show()

Source: https://steamcommunity.com/sharedfiles/filedetails/?id=2705959249					

More Bitburner guilds