forked from YearOfProgramming/2017Challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[python]challenge_[6](Pending) (YearOfProgramming#225)
* appended readme * typo fix * Update Solution.py * Update README.md * updated readme * [Python]Challenge_7(Unreviewed)
- Loading branch information
Showing
7 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#Solution | ||
|
||
I think I got it. All my code does is stop everytime the range in the list breaks and puts the start and end of that range into | ||
a list and then returns that list. Of course there are a couple of statements that check for special cases, but overall it seems | ||
ok to me. I did get stuck on understanding what the prompt meant by ranges untill I noticed that it was just consecutive numbers... | ||
whoops! Either way, it's all done now and I implemented a unit test of the given test cases and one really long one I made. | ||
|
||
If you'd like to use my unit tests, all you have to do is implement your solution in my Solution.py. Change the code under the compRange function using the input parameters given. Copy my test.py and Solution.py before you edit them. Please don't make a pull request that writes over the code in my file. | ||
|
||
`Marcus Jefferson` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Writing solution for this range problem | ||
|
||
class solution(object): | ||
def compRange(self,nums): | ||
# nums is of type list() | ||
# nums contains only integers in ascending order | ||
# this function should return the list of collapsed ranges | ||
|
||
if nums == None: | ||
return None | ||
if len(nums) == 1: | ||
return [] | ||
|
||
resList = list() | ||
start = nums[0] | ||
end = 0 | ||
newList = list() | ||
for i in xrange(1,len(nums)): | ||
if nums[i] != int(nums[i-1] + 1): | ||
end = nums[i-1] | ||
resList.append("{s}->{e}".format(s = start, e = end)) | ||
start = nums[i] | ||
|
||
if i == len(nums) - 1: | ||
end = nums[i] | ||
resList.append("{s}->{e}".format(s = start, e = end)) | ||
|
||
return resList |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#Solution | ||
|
||
I think I got it figured out. All I had to do was take the sum of the length of the list and then the sum of all the numbers in the list, find the difference betweend those results, and then return the difference modded by the length of the list. The function meets runtime and space requirements. | ||
|
||
I included some test cases in my solution. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
def solution(nums): | ||
n = len(nums) + 1 | ||
sums = 0 | ||
Summation = 0 | ||
for i in nums: | ||
sums += i | ||
for i in xrange(0,n+1): | ||
Summation += i | ||
return (Summation - sums) % n |
Large diffs are not rendered by default.
Oops, something went wrong.