Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 711a78b

Browse files
author
pranbhat
committed
Adding formatting and also morris order traversals
1 parent c27caf1 commit 711a78b

18 files changed

+572
-45
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.pyc
2+
./.vscode/.ropeproject/

.vscode/.ropeproject/config.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# The default ``config.py``
2+
# flake8: noqa
3+
4+
5+
def set_prefs(prefs):
6+
"""This function is called before opening the project"""
7+
8+
# Specify which files and folders to ignore in the project.
9+
# Changes to ignored resources are not added to the history and
10+
# VCSs. Also they are not returned in `Project.get_files()`.
11+
# Note that ``?`` and ``*`` match all characters but slashes.
12+
# '*.pyc': matches 'test.pyc' and 'pkg/test.pyc'
13+
# 'mod*.pyc': matches 'test/mod1.pyc' but not 'mod/1.pyc'
14+
# '.svn': matches 'pkg/.svn' and all of its children
15+
# 'build/*.o': matches 'build/lib.o' but not 'build/sub/lib.o'
16+
# 'build//*.o': matches 'build/lib.o' and 'build/sub/lib.o'
17+
prefs['ignored_resources'] = ['*.pyc', '*~', '.ropeproject',
18+
'.hg', '.svn', '_svn', '.git', '.tox']
19+
20+
# Specifies which files should be considered python files. It is
21+
# useful when you have scripts inside your project. Only files
22+
# ending with ``.py`` are considered to be python files by
23+
# default.
24+
# prefs['python_files'] = ['*.py']
25+
26+
# Custom source folders: By default rope searches the project
27+
# for finding source folders (folders that should be searched
28+
# for finding modules). You can add paths to that list. Note
29+
# that rope guesses project source folders correctly most of the
30+
# time; use this if you have any problems.
31+
# The folders should be relative to project root and use '/' for
32+
# separating folders regardless of the platform rope is running on.
33+
# 'src/my_source_folder' for instance.
34+
# prefs.add('source_folders', 'src')
35+
36+
# You can extend python path for looking up modules
37+
# prefs.add('python_path', '~/python/')
38+
39+
# Should rope save object information or not.
40+
prefs['save_objectdb'] = True
41+
prefs['compress_objectdb'] = False
42+
43+
# If `True`, rope analyzes each module when it is being saved.
44+
prefs['automatic_soa'] = True
45+
# The depth of calls to follow in static object analysis
46+
prefs['soa_followed_calls'] = 0
47+
48+
# If `False` when running modules or unit tests "dynamic object
49+
# analysis" is turned off. This makes them much faster.
50+
prefs['perform_doa'] = True
51+
52+
# Rope can check the validity of its object DB when running.
53+
prefs['validate_objectdb'] = True
54+
55+
# How many undos to hold?
56+
prefs['max_history_items'] = 32
57+
58+
# Shows whether to save history across sessions.
59+
prefs['save_history'] = True
60+
prefs['compress_history'] = False
61+
62+
# Set the number spaces used for indenting. According to
63+
# :PEP:`8`, it is best to use 4 spaces. Since most of rope's
64+
# unit-tests use 4 spaces it is more reliable, too.
65+
prefs['indent_size'] = 4
66+
67+
# Builtin and c-extension modules that are allowed to be imported
68+
# and inspected by rope.
69+
prefs['extension_modules'] = []
70+
71+
# Add all standard c-extensions to extension_modules list.
72+
prefs['import_dynload_stdmods'] = True
73+
74+
# If `True` modules with syntax errors are considered to be empty.
75+
# The default value is `False`; When `False` syntax errors raise
76+
# `rope.base.exceptions.ModuleSyntaxError` exception.
77+
prefs['ignore_syntax_errors'] = False
78+
79+
# If `True`, rope ignores unresolvable imports. Otherwise, they
80+
# appear in the importing namespace.
81+
prefs['ignore_bad_imports'] = False
82+
83+
# If `True`, rope will insert new module imports as
84+
# `from <package> import <module>` by default.
85+
prefs['prefer_module_from_imports'] = False
86+
87+
# If `True`, rope will transform a comma list of imports into
88+
# multiple separate import statements when organizing
89+
# imports.
90+
prefs['split_imports'] = False
91+
92+
# If `True`, rope will remove all top-level import statements and
93+
# reinsert them at the top of the module when making changes.
94+
prefs['pull_imports_to_top'] = True
95+
96+
# If `True`, rope will sort imports alphabetically by module name instead
97+
# of alphabetically by import statement, with from imports after normal
98+
# imports.
99+
prefs['sort_imports_alphabetically'] = False
100+
101+
# Location of implementation of
102+
# rope.base.oi.type_hinting.interfaces.ITypeHintingFactory In general
103+
# case, you don't have to change this value, unless you're an rope expert.
104+
# Change this value to inject you own implementations of interfaces
105+
# listed in module rope.base.oi.type_hinting.providers.interfaces
106+
# For example, you can add you own providers for Django Models, or disable
107+
# the search type-hinting in a class hierarchy, etc.
108+
prefs['type_hinting_factory'] = (
109+
'rope.base.oi.type_hinting.factory.default_type_hinting_factory')
110+
111+
112+
def project_opened(project):
113+
"""This function is called after opening the project"""
114+
# Do whatever you like here!

.vscode/.ropeproject/objectdb

6 Bytes
Binary file not shown.

.vscode/launch.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Python: Current File",
9+
"type": "python",
10+
"request": "launch",
11+
"program": "${file}",
12+
"console": "integratedTerminal",
13+
"cwd": "${workspaceRoot}",
14+
"env": {"PYTHONPATH": "${workspaceRoot}"},
15+
}
16+
]
17+
}

.vscode/settings.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"python.jediEnabled": true,
3+
"editor.tabSize": 4,
4+
"editor.insertSpaces": true,
5+
"editor.formatOnSave": true
6+
}

ideserve_online/binaryTreeNode.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class BinaryTreeNode:
2+
def __init__(self, data, left=None, right=None):
3+
self.data = data
4+
self.left = left
5+
self.right = right

ideserve_online/findElementInArrayOccuringOddTimes.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,22 @@
1414
Output: 4
1515
'''
1616

17+
1718
def findElementInArrayOccuringOddNumberOfTimes(arr):
18-
return functools.reduce(lambda x,y : x^y, arr, 0)
19+
return functools.reduce(lambda x, y: x ^ y, arr, 0)
1920

2021

2122
class CodeTest(unittest.TestCase):
2223
def testEmptyArray(self):
2324
self.assertEqual(findElementInArrayOccuringOddNumberOfTimes([]), 0)
2425

2526
def testAllEvenCountArray(self):
26-
self.assertEqual(findElementInArrayOccuringOddNumberOfTimes([1,1,3,3,2,2,6,6,4,4,3,3,5,5,7,7]), 0)
27+
self.assertEqual(findElementInArrayOccuringOddNumberOfTimes(
28+
[1, 1, 3, 3, 2, 2, 6, 6, 4, 4, 3, 3, 5, 5, 7, 7]), 0)
2729

2830
def testSingleElementOddCountArray(self):
29-
self.assertEqual(findElementInArrayOccuringOddNumberOfTimes([1,1,3,3,2,2,6,6,4,4,3,3,5,5,5,7,7]), 5)
31+
self.assertEqual(findElementInArrayOccuringOddNumberOfTimes(
32+
[1, 1, 3, 3, 2, 2, 6, 6, 4, 4, 3, 3, 5, 5, 5, 7, 7]), 5)
3033

3134

32-
unittest.main()
35+
unittest.main()

ideserve_online/findElementWhichOccursOnceWhileOthersOccurThrice.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,26 @@ def findElementWhichOccursOnceWhileOthersOccurThrice(arr):
2121
if not maxVal:
2222
return None
2323

24-
p = 1
24+
p = 1
2525
bitCount = 0
2626
while p < maxVal:
2727
bitCount += 1
28-
p<<=1
29-
28+
p <<= 1
29+
3030
bits = [0] * bitCount
3131
for elem in arr:
3232
v = 1
3333
for i in range(len(bits)):
3434
if v & elem:
35-
bits[i]+=1
36-
v<<=1
37-
bits = map(lambda x: x%3, bits)
35+
bits[i] += 1
36+
v <<= 1
37+
bits = list(map(lambda x: x % 3, bits))
3838
missingVal = 0
3939
for i in range(len(bits)):
4040
if bits[i] > 1:
4141
return None
4242
if bits[i]:
43-
missingVal+=(2**i)
43+
missingVal += (2**i)
4444
return missingVal
4545

4646

@@ -49,17 +49,22 @@ def testEmpty(self):
4949
self.assertIsNone(findElementWhichOccursOnceWhileOthersOccurThrice([]))
5050

5151
def testIncorrectLength(self):
52-
self.assertIsNone(findElementWhichOccursOnceWhileOthersOccurThrice([1,1,1,2,2,5,5,5,3,3,3]))
52+
self.assertIsNone(findElementWhichOccursOnceWhileOthersOccurThrice(
53+
[1, 1, 1, 2, 2, 5, 5, 5, 3, 3, 3]))
5354

5455
def testTwoElementsOccuringTwice(self):
5556
# note the length is still 3x + 1
56-
self.assertIsNone(findElementWhichOccursOnceWhileOthersOccurThrice([1,1,1,2,2,5,5,5,3,3,3, 4, 4]))
57+
self.assertIsNone(findElementWhichOccursOnceWhileOthersOccurThrice(
58+
[1, 1, 1, 2, 2, 5, 5, 5, 3, 3, 3, 4, 4]))
5759

5860
def testMaxZero(self):
5961
# note the length is still 3x + 1
60-
self.assertIsNone(findElementWhichOccursOnceWhileOthersOccurThrice([0,0,0,0,0,0,0]))
62+
self.assertIsNone(
63+
findElementWhichOccursOnceWhileOthersOccurThrice([0, 0, 0, 0, 0, 0, 0]))
6164

6265
def testOnlyOneElementOccursOnce(self):
63-
self.assertEqual(findElementWhichOccursOnceWhileOthersOccurThrice([1,1,1,2,5,5,5,3,3,3]), 2)
66+
self.assertEqual(findElementWhichOccursOnceWhileOthersOccurThrice(
67+
[1, 1, 1, 2, 5, 5, 5, 3, 3, 3]), 2)
68+
6469

6570
unittest.main()

ideserve_online/findElementWhichOccursXTimesWhileOthersOccurYTimes.py

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,49 +26,58 @@ def findElementWhichOccursXTimesWhileOthersOccurYTimes(arr, x, y):
2626
if not maxVal:
2727
return None
2828

29-
p = 1
29+
p = 1
3030
bitCount = 0
3131
while p <= maxVal:
3232
bitCount += 1
33-
p<<=1
34-
33+
p <<= 1
34+
3535
bits = [0] * bitCount
3636
for elem in arr:
3737
v = 1
3838
for i in range(len(bits)):
3939
if v & elem:
40-
bits[i]+=1
41-
v<<=1
40+
bits[i] += 1
41+
v <<= 1
4242

43-
bits = map(lambda a: a%x, bits)
43+
bits = list(map(lambda a: a % x, bits))
4444
missingVal = 0
4545
for i in range(len(bits)):
4646
if bits[i] != expectedBitCount and bits[i] != 0:
4747
return None
4848
if bits[i]:
49-
missingVal+=(2**i)
49+
missingVal += (2**i)
5050
return missingVal
5151

5252

5353
class CodeTest(unittest.TestCase):
5454
def testEmpty(self):
55-
self.assertIsNone(findElementWhichOccursXTimesWhileOthersOccurYTimes([], 5, 2))
56-
55+
self.assertIsNone(
56+
findElementWhichOccursXTimesWhileOthersOccurYTimes([], 5, 2))
57+
5758
def testXIsSameAsY(self):
58-
self.assertIsNone(findElementWhichOccursXTimesWhileOthersOccurYTimes([1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,6,6,6,6,6,5,5,5,5,5], 5, 5))
59+
self.assertIsNone(findElementWhichOccursXTimesWhileOthersOccurYTimes(
60+
[1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 6, 6, 6, 6, 6, 5, 5, 5, 5, 5], 5, 5))
5961

6062
def testIncorrectLengthWhenXGreaterThanY(self):
61-
self.assertIsNone(findElementWhichOccursXTimesWhileOthersOccurYTimes([1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,6,6,6,6,6,5,5,5,5,5, 8, 8, 8], 5, 2))
63+
self.assertIsNone(findElementWhichOccursXTimesWhileOthersOccurYTimes(
64+
[1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 6, 6, 6, 6, 6, 5, 5, 5, 5, 5, 8, 8, 8], 5, 2))
6265

6366
def testIncorrectLengthWhenXLessThanY(self):
64-
self.assertIsNone(findElementWhichOccursXTimesWhileOthersOccurYTimes([1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,6,6,6,6,6,5,5,5,5,5, 8, 8, 8], 5, 7))
67+
self.assertIsNone(findElementWhichOccursXTimesWhileOthersOccurYTimes(
68+
[1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 6, 6, 6, 6, 6, 5, 5, 5, 5, 5, 8, 8, 8], 5, 7))
6569

6670
def testMaxZeroWhenXLessThanY(self):
67-
self.assertIsNone(findElementWhichOccursXTimesWhileOthersOccurYTimes([0] * 12, 5, 7))
71+
self.assertIsNone(
72+
findElementWhichOccursXTimesWhileOthersOccurYTimes([0] * 12, 5, 7))
6873

69-
def testCorrectInputXLessThanY(self):
70-
self.assertEqual(findElementWhichOccursXTimesWhileOthersOccurYTimes([1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,6,6,6,6,6,5,5,5,5,5, 8, 8, 8], 5, 3), 8)
74+
def testCorrectInputXGreaterThanY(self):
75+
self.assertEqual(findElementWhichOccursXTimesWhileOthersOccurYTimes(
76+
[1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 6, 6, 6, 6, 6, 5, 5, 5, 5, 5, 8, 8, 8], 5, 3), 8)
7177

7278
def testCorrectInputXLessThanY(self):
73-
self.assertEqual(findElementWhichOccursXTimesWhileOthersOccurYTimes([1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,6,6,6,6,6,5,5,5,5,5, 8, 8, 8, 8, 8, 8, 8, 8], 5, 8), 8)
79+
self.assertEqual(findElementWhichOccursXTimesWhileOthersOccurYTimes(
80+
[1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 6, 6, 6, 6, 6, 5, 5, 5, 5, 5, 8, 8, 8, 8, 8, 8, 8, 8], 5, 8), 8)
81+
82+
7483
unittest.main()

ideserve_online/findMissingElementInDuplicateArray.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,34 @@
1616
7
1717
'''
1818

19+
1920
def findMissingElementInDuplicateArray(arr, duplicate):
2021
if not len(arr) or len(arr) != len(duplicate) + 1:
2122
return None
22-
missing = functools.reduce(lambda x,y: x^y, arr+duplicate, 0)
23+
missing = functools.reduce(lambda x, y: x ^ y, arr+duplicate, 0)
2324
if missing or (missing in arr and missing not in duplicate):
2425
return missing
25-
else:
26-
raise None
26+
return None
27+
2728

2829
class CodeTest(unittest.TestCase):
2930
def testEmptyArray(self):
3031
self.assertIsNone(findMissingElementInDuplicateArray([], []))
31-
32-
32+
3333
def testInvalidDup(self):
34-
self.assertIsNone(findMissingElementInDuplicateArray([1,2, 3], [1]))
34+
self.assertIsNone(findMissingElementInDuplicateArray([1, 2, 3], [1]))
3535

3636
def testInvalidLargerDup(self):
37-
self.assertIsNone(findMissingElementInDuplicateArray([1,2, 3], [1, 2, 3]))
37+
self.assertIsNone(
38+
findMissingElementInDuplicateArray([1, 2, 3], [1, 2, 3]))
3839

3940
def testZeroAsAnswer(self):
40-
self.assertEqual(findMissingElementInDuplicateArray([1,2, 3, 0], [1, 2, 3]), 0)
41+
self.assertEqual(findMissingElementInDuplicateArray(
42+
[1, 2, 3, 0], [1, 2, 3]), 0)
4143

4244
def testNonZeroAsValidAnswer(self):
43-
self.assertEqual(findMissingElementInDuplicateArray([1,2, 3, 4], [1, 2, 3]), 4)
45+
self.assertEqual(findMissingElementInDuplicateArray(
46+
[1, 2, 3, 4], [1, 2, 3]), 4)
47+
4448

4549
unittest.main()
46-

0 commit comments

Comments
 (0)