Skip to content

Commit 1f1811b

Browse files
committed
Task 0009 Added.
Solution to 0001 updated.
1 parent b0dd008 commit 1f1811b

File tree

4 files changed

+207
-3
lines changed

4 files changed

+207
-3
lines changed

.idea/LearnPython.iml

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Tasks/0001.ipynb

+153-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"cells": [
33
{
4-
"attachments": {},
54
"cell_type": "markdown",
65
"metadata": {},
76
"source": [
@@ -20,6 +19,159 @@
2019
"8. Input: 12,34,567.89 --- Output: 76,54,321.98\n",
2120
"9. Input: -12,34,567.89 --- Output: -76,54,321.98"
2221
]
22+
},
23+
{
24+
"cell_type": "code",
25+
"execution_count": 2,
26+
"metadata": {},
27+
"outputs": [
28+
{
29+
"name": "stdout",
30+
"output_type": "stream",
31+
"text": [
32+
"Enter your number : -123,45,67.89\n",
33+
"-76,54,321.98\n"
34+
]
35+
}
36+
],
37+
"source": [
38+
"# Solution provided by --- Krat0s\n",
39+
"\n",
40+
"def data(num):\n",
41+
" try:\n",
42+
" return int(num)\n",
43+
" except ValueError:\n",
44+
" try:\n",
45+
" return float(num)\n",
46+
" except ValueError:\n",
47+
" try:\n",
48+
" return str(num)\n",
49+
" except ValueError:\n",
50+
" return num\n",
51+
"\n",
52+
"def int_rev(num):\n",
53+
" new=None\n",
54+
" l=str(num)\n",
55+
" #to reverse if number is greater than zero\n",
56+
" if num>0:\n",
57+
" return int(l[::-1])\n",
58+
" #to reverse if number is less than zero\n",
59+
" new='-'+l[:0:-1]\n",
60+
" return int(new)\n",
61+
"\n",
62+
"def before_dec_rev(num):\n",
63+
" s,a=\"\",0\n",
64+
" n=str(num)\n",
65+
" while(n[a]!='.'):\n",
66+
" s+=n[a]\n",
67+
" a+=1\n",
68+
" if num>0:\n",
69+
" return float(s[::-1]+n[a:])\n",
70+
" return float('-'+s[:0:-1] + n[a:])\n",
71+
"\n",
72+
"def if_str(num):\n",
73+
" nn=new_str=fn=''\n",
74+
" a=0\n",
75+
" n=num.replace(',','')\n",
76+
" if type(data(n)) is int:\n",
77+
" if data(n)>0:\n",
78+
" nn=n[::-1]\n",
79+
" new_str=nn[:2]+','+nn[2:4]+','+nn[4:]\n",
80+
" if data(n)<0:\n",
81+
" nn = n[:0:-1]\n",
82+
" new_str ='-'+nn[:2] + ',' + nn[2:4] + ',' + nn[4:]\n",
83+
" elif type(data(n)) is float:\n",
84+
" if data(n)>0:\n",
85+
" while(n[a]!='.'):\n",
86+
" nn+=n[a]\n",
87+
" a+=1\n",
88+
" fn=nn[::-1]\n",
89+
" nn=n[-2:]\n",
90+
" fn+=nn[::-1]\n",
91+
" new_str=fn[:2]+','+fn[2:4]+','+fn[4:7]+'.'+fn[7:]\n",
92+
" if data(n)<0:\n",
93+
" while (n[a] != '.'):\n",
94+
" nn += n[a]\n",
95+
" a += 1\n",
96+
" fn = nn[:0:-1]\n",
97+
" nn = n[-2:]\n",
98+
" fn += nn[::-1]\n",
99+
" new_str ='-'+fn[:2] + ',' + fn[2:4] + ',' + fn[4:7] + '.' + fn[7:]\n",
100+
" else:\n",
101+
" new_str=\"Only Numbers Accepted!\"\n",
102+
" return new_str\n",
103+
"\n",
104+
"num=input(\"Enter your number : \")\n",
105+
"n=data(num)\n",
106+
"if type(n) is int:\n",
107+
" print(int_rev(int(n)))\n",
108+
"elif type(n) is float:\n",
109+
" print(before_dec_rev(float(n)))\n",
110+
"else:\n",
111+
" print(if_str(n))"
112+
]
113+
},
114+
{
115+
"cell_type": "code",
116+
"execution_count": 8,
117+
"metadata": {},
118+
"outputs": [
119+
{
120+
"name": "stdout",
121+
"output_type": "stream",
122+
"text": [
123+
"Enter a number:123\n",
124+
"321\n"
125+
]
126+
}
127+
],
128+
"source": [
129+
"# Solution provided by --- Avisek Mayur\n",
130+
"\n",
131+
"def main():\n",
132+
" inp=input(\"Enter a number:\")\n",
133+
" sign=str1=outp=''\n",
134+
" point=comma=False\n",
135+
" if(inp[0]=='-'):\n",
136+
" sign='-'\n",
137+
" inp=inp[1:]\n",
138+
" for i in inp:\n",
139+
" if(i.isdigit()):\n",
140+
" str1=i+str1\n",
141+
" elif(i=='.'):\n",
142+
" if(point):\n",
143+
" str1=outp=sign=''\n",
144+
" comma=point=False\n",
145+
" str1='Multiple points are not allowed'\n",
146+
" break\n",
147+
" point=True\n",
148+
" outp=str1+'.'\n",
149+
" str1=''\n",
150+
" elif(i==','):\n",
151+
" comma=True\n",
152+
" else:#\n",
153+
" str1=outp=sign=''\n",
154+
" comma=point=False\n",
155+
" str1='Only numbers expected'\n",
156+
" break\n",
157+
" \n",
158+
" if(comma):\n",
159+
" if(point):\n",
160+
" size=len(outp)-1\n",
161+
" else:\n",
162+
" outp=str1\n",
163+
" str1=''\n",
164+
" size=len(outp)\n",
165+
" count=0\n",
166+
" for i in range(2,size-2,2):\n",
167+
" outp=outp[:i+count]+','+outp[i+count:]\n",
168+
" count+=1\n",
169+
" \n",
170+
" outp=outp+str1\n",
171+
" print(sign+outp)\n",
172+
"\n",
173+
"main()"
174+
]
23175
}
24176
],
25177
"metadata": {

Tasks/0009.ipynb

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Practice Code: 0009\n",
8+
"\n",
9+
"**Task:<br><br>\n",
10+
"Given a numerical array. Check if there are two such numbers in the array, multiplying which we get the given number X.**\n",
11+
"\n",
12+
"Come up with an optimal algorithm and implement it. Use least amount of variables and statements, but performance matters.\n",
13+
"\n",
14+
"Array: [1, 2, 3, 4, 5, 6]\n",
15+
"Input: 10 --- Output: (2,5)\n",
16+
"Input: 12 --- Output: (2,6),(3,4)\n",
17+
"\n",
18+
"#python #practiceCode #samples #interviewQuestions #collections #algorithm"
19+
]
20+
},
21+
{
22+
"cell_type": "code",
23+
"execution_count": null,
24+
"metadata": {},
25+
"outputs": [],
26+
"source": [
27+
"# Solution will be posted shortly"
28+
]
29+
}
30+
],
31+
"metadata": {
32+
"kernelspec": {
33+
"display_name": "Python 3",
34+
"language": "python",
35+
"name": "python3"
36+
},
37+
"language_info": {
38+
"codemirror_mode": {
39+
"name": "ipython",
40+
"version": 3
41+
},
42+
"file_extension": ".py",
43+
"mimetype": "text/x-python",
44+
"name": "python",
45+
"nbconvert_exporter": "python",
46+
"pygments_lexer": "ipython3",
47+
"version": "3.7.0"
48+
}
49+
},
50+
"nbformat": 4,
51+
"nbformat_minor": 2
52+
}

0 commit comments

Comments
 (0)