mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Move xor challenge solution to a class (#143)
This commit is contained in:
parent
098aaf196d
commit
cf48629858
|
@ -72,9 +72,11 @@
|
|||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def max_xor(lower, upper):\n",
|
||||
" # TODO: Implement me\n",
|
||||
" pass"
|
||||
"class Solution(object):\n",
|
||||
"\n",
|
||||
" def max_xor(self, lower, upper):\n",
|
||||
" # TODO: Implement me\n",
|
||||
" pass"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -101,7 +103,8 @@
|
|||
"class TestMaximingXor(object):\n",
|
||||
"\n",
|
||||
" def test_maximizing_xor(self):\n",
|
||||
" assert_equal(max_xor(10, 15), 7)\n",
|
||||
" solution = Solution()\n",
|
||||
" assert_equal(solution.max_xor(10, 15), 7)\n",
|
||||
" print('Success: test_maximizing_xor')\n",
|
||||
"\n",
|
||||
"\n",
|
||||
|
@ -126,21 +129,21 @@
|
|||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 2",
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python2"
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 2
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython2",
|
||||
"version": "2.7.10"
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.5.0"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
|
|
@ -82,14 +82,16 @@
|
|||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def max_xor(lower, upper):\n",
|
||||
" result = 0\n",
|
||||
" for l in range(lower, upper + 1):\n",
|
||||
" for u in range(lower, upper + 1):\n",
|
||||
" curr = l ^ u\n",
|
||||
" if result < curr:\n",
|
||||
" result = curr\n",
|
||||
" return result"
|
||||
"class Solution(object):\n",
|
||||
"\n",
|
||||
" def max_xor(self, lower, upper):\n",
|
||||
" result = 0\n",
|
||||
" for l in range(lower, upper + 1):\n",
|
||||
" for u in range(lower, upper + 1):\n",
|
||||
" curr = l ^ u\n",
|
||||
" if result < curr:\n",
|
||||
" result = curr\n",
|
||||
" return result"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -123,7 +125,8 @@
|
|||
"class TestMaximingXor(object):\n",
|
||||
"\n",
|
||||
" def test_maximizing_xor(self):\n",
|
||||
" assert_equal(max_xor(10, 15), 7)\n",
|
||||
" solution = Solution()\n",
|
||||
" assert_equal(solution.max_xor(10, 15), 7)\n",
|
||||
" print('Success: test_maximizing_xor')\n",
|
||||
"\n",
|
||||
"\n",
|
||||
|
@ -158,21 +161,21 @@
|
|||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 2",
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python2"
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 2
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython2",
|
||||
"version": "2.7.10"
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.5.0"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
|
|
@ -4,7 +4,8 @@ from nose.tools import assert_equal
|
|||
class TestMaximingXor(object):
|
||||
|
||||
def test_maximizing_xor(self):
|
||||
assert_equal(max_xor(10, 15), 7)
|
||||
solution = Solution()
|
||||
assert_equal(solution.max_xor(10, 15), 7)
|
||||
print('Success: test_maximizing_xor')
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user