Guest User

Untitled

a guest
Jan 18th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.14 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "Write code to check a String is palindrome or not"
  8. ]
  9. },
  10. {
  11. "cell_type": "code",
  12. "execution_count": 67,
  13. "metadata": {
  14. "collapsed": true
  15. },
  16. "outputs": [],
  17. "source": [
  18. "def palind_check(x):\n",
  19. " a = x.replace(' ','').lower()\n",
  20. " if a == a[::-1]:\n",
  21. " return True\n",
  22. " else:\n",
  23. " return False"
  24. ]
  25. },
  26. {
  27. "cell_type": "code",
  28. "execution_count": 68,
  29. "metadata": {},
  30. "outputs": [
  31. {
  32. "data": {
  33. "text/plain": [
  34. "True"
  35. ]
  36. },
  37. "execution_count": 68,
  38. "metadata": {},
  39. "output_type": "execute_result"
  40. }
  41. ],
  42. "source": [
  43. "palind_check('A Santa dog lived as a devil God at NASA')"
  44. ]
  45. },
  46. {
  47. "cell_type": "code",
  48. "execution_count": 69,
  49. "metadata": {},
  50. "outputs": [
  51. {
  52. "data": {
  53. "text/plain": [
  54. "True"
  55. ]
  56. },
  57. "execution_count": 69,
  58. "metadata": {},
  59. "output_type": "execute_result"
  60. }
  61. ],
  62. "source": [
  63. "palind_check('Nittin')"
  64. ]
  65. },
  66. {
  67. "cell_type": "markdown",
  68. "metadata": {},
  69. "source": [
  70. "Write a method which will remove any given character from a String? "
  71. ]
  72. },
  73. {
  74. "cell_type": "code",
  75. "execution_count": 5,
  76. "metadata": {
  77. "collapsed": true
  78. },
  79. "outputs": [],
  80. "source": [
  81. "a = 'this is a %sentence'\n",
  82. "def remove_character(x,index):\n",
  83. " return x.replace(x[index],'')"
  84. ]
  85. },
  86. {
  87. "cell_type": "code",
  88. "execution_count": 6,
  89. "metadata": {},
  90. "outputs": [
  91. {
  92. "data": {
  93. "text/plain": [
  94. "'this is a sentence'"
  95. ]
  96. },
  97. "execution_count": 6,
  98. "metadata": {},
  99. "output_type": "execute_result"
  100. }
  101. ],
  102. "source": [
  103. "remove_character(a,10)"
  104. ]
  105. },
  106. {
  107. "cell_type": "markdown",
  108. "metadata": {},
  109. "source": [
  110. "In an list 1-100 numbers are stored, one number is missing how do you find it?"
  111. ]
  112. },
  113. {
  114. "cell_type": "code",
  115. "execution_count": 54,
  116. "metadata": {},
  117. "outputs": [
  118. {
  119. "data": {
  120. "text/plain": [
  121. "75"
  122. ]
  123. },
  124. "execution_count": 54,
  125. "metadata": {},
  126. "output_type": "execute_result"
  127. }
  128. ],
  129. "source": [
  130. "a = list(range(101))\n",
  131. "a.pop(75)\n",
  132. "sum(range(101)) - sum(a)"
  133. ]
  134. },
  135. {
  136. "cell_type": "markdown",
  137. "metadata": {},
  138. "source": [
  139. "In an list 1-100 exactly one number is duplicate how do you find it?"
  140. ]
  141. },
  142. {
  143. "cell_type": "code",
  144. "execution_count": 52,
  145. "metadata": {},
  146. "outputs": [
  147. {
  148. "data": {
  149. "text/plain": [
  150. "35"
  151. ]
  152. },
  153. "execution_count": 52,
  154. "metadata": {},
  155. "output_type": "execute_result"
  156. }
  157. ],
  158. "source": [
  159. "y = list(range(101))\n",
  160. "y.append(35)\n",
  161. "sum(y) - sum (range(101))"
  162. ]
  163. },
  164. {
  165. "cell_type": "markdown",
  166. "metadata": {},
  167. "source": [
  168. "Given two list, 1,2,3,4,5 and 2,3,1,0,5 find which number is not present in the second list?"
  169. ]
  170. },
  171. {
  172. "cell_type": "code",
  173. "execution_count": 55,
  174. "metadata": {
  175. "collapsed": true
  176. },
  177. "outputs": [],
  178. "source": [
  179. "a = [1,2,3,4,5]\n",
  180. "b = [2,3,1,0,5]"
  181. ]
  182. },
  183. {
  184. "cell_type": "code",
  185. "execution_count": 57,
  186. "metadata": {},
  187. "outputs": [
  188. {
  189. "name": "stdout",
  190. "output_type": "stream",
  191. "text": [
  192. "4\n"
  193. ]
  194. }
  195. ],
  196. "source": [
  197. "list(set(a) - set(b)) # Difference a- b will return the value only in a and b -a will return the value only present in b\n",
  198. "\"\"\" OR\"\"\"\n",
  199. "\n",
  200. "for i in a:\n",
  201. " if i not in b:\n",
  202. " print (i)"
  203. ]
  204. },
  205. {
  206. "cell_type": "markdown",
  207. "metadata": {},
  208. "source": [
  209. "How do you find second highest number in an integer array?"
  210. ]
  211. },
  212. {
  213. "cell_type": "code",
  214. "execution_count": 58,
  215. "metadata": {},
  216. "outputs": [
  217. {
  218. "data": {
  219. "text/plain": [
  220. "18"
  221. ]
  222. },
  223. "execution_count": 58,
  224. "metadata": {},
  225. "output_type": "execute_result"
  226. }
  227. ],
  228. "source": [
  229. "z = list(range(20))\n",
  230. "sorted(set(z),reverse = True)[1]\n",
  231. "\n",
  232. "\"\"\" OR \"\"\"\n",
  233. "z.sort(reverse = True)\n",
  234. "z[1]"
  235. ]
  236. },
  237. {
  238. "cell_type": "markdown",
  239. "metadata": {},
  240. "source": [
  241. "How to swap two numbers without using temp variable?"
  242. ]
  243. },
  244. {
  245. "cell_type": "code",
  246. "execution_count": 62,
  247. "metadata": {},
  248. "outputs": [
  249. {
  250. "name": "stdout",
  251. "output_type": "stream",
  252. "text": [
  253. "64 24\n"
  254. ]
  255. }
  256. ],
  257. "source": [
  258. "a = 24\n",
  259. "b = 64\n",
  260. "a,b = b,a \n",
  261. "print (a,b)"
  262. ]
  263. },
  264. {
  265. "cell_type": "code",
  266. "execution_count": null,
  267. "metadata": {},
  268. "outputs": [],
  269. "source": []
  270. }
  271. ],
  272. "metadata": {
  273. "kernelspec": {
  274. "display_name": "Python 3",
  275. "language": "python",
  276. "name": "python3"
  277. },
  278. "language_info": {
  279. "codemirror_mode": {
  280. "name": "ipython",
  281. "version": 3
  282. },
  283. "file_extension": ".py",
  284. "mimetype": "text/x-python",
  285. "name": "python",
  286. "nbconvert_exporter": "python",
  287. "pygments_lexer": "ipython3",
  288. "version": "3.6.3"
  289. }
  290. },
  291. "nbformat": 4,
  292. "nbformat_minor": 2
  293. }
Add Comment
Please, Sign In to add comment