cracker/index.md

39 lines
985 B
Markdown
Raw Normal View History

2018-11-26 09:08:39 +00:00
2016-10-06 23:01:49 +00:00
---
layout: default
---
2018-11-26 09:08:39 +00:00
# Begin.
2016-10-06 23:01:49 +00:00
2018-11-26 09:08:39 +00:00
Sometimes you write a code, this code simple actually and not hard to think about it. When you finish the code, you realize something. That code amazing. First of all code is working okay :) and you feel it's like a magic. Maybe it could be better, but for now this is the best.
2016-10-06 23:01:49 +00:00
2018-11-26 09:08:39 +00:00
```python
#!/usr/bin/python3
2016-10-06 23:01:49 +00:00
2018-11-26 09:08:39 +00:00
# Given array has some integers.
2016-10-06 23:01:49 +00:00
2018-11-26 09:08:39 +00:00
# We are trying to find max difference between an integer in array and situated in array more lower index integers.
2016-10-06 23:01:49 +00:00
2018-11-26 09:08:39 +00:00
# If difference under 0 result should be -1.
2016-10-06 23:01:49 +00:00
2018-11-26 09:08:39 +00:00
def maxDifference(a):
max_difference = -1
for i1 in range(len(a)):
2016-10-06 23:01:49 +00:00
2018-11-26 09:08:39 +00:00
for i2 in range(i1):
difference = a[i1] - a[i2]
if difference > 0 and difference > max_difference:
max_difference = difference
2016-10-06 23:01:49 +00:00
2018-11-26 09:08:39 +00:00
return max_difference
2016-10-06 23:01:49 +00:00
2018-11-26 09:08:39 +00:00
if __name__ == '__main__':
2016-10-06 23:01:49 +00:00
2018-11-26 09:08:39 +00:00
a = [6, 5, 10, 2, 1, 9, 3]
print(maxDifference(a)) # => Result 8 because for this example, max difference between 9 - 1 = 8.
2016-10-06 23:01:49 +00:00
```