Skip to content

Commit 82ca4ac

Browse files
SUDO PALCEMT 2019
1 parent 5b0a500 commit 82ca4ac

File tree

1 file changed

+133
-7
lines changed

1 file changed

+133
-7
lines changed

GeeksForGeeks/Sudo Placement 2019/bit-magic/bit-magic.ipynb

Lines changed: 133 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,16 +1014,17 @@
10141014
},
10151015
{
10161016
"cell_type": "code",
1017-
"execution_count": 21,
1017+
"execution_count": 1,
10181018
"metadata": {},
10191019
"outputs": [
10201020
{
10211021
"name": "stdout",
10221022
"output_type": "stream",
10231023
"text": [
10241024
"1\n",
1025-
"1 3 5\n",
1026-
"8\n"
1025+
"8\n",
1026+
"-1865008399 -524833575 -1162539990 -1003374718 -1677272376 -2046456104 -689632770 -688705725\n",
1027+
"854\n"
10271028
]
10281029
}
10291030
],
@@ -1054,6 +1055,110 @@
10541055
" print(count)"
10551056
]
10561057
},
1058+
{
1059+
"cell_type": "code",
1060+
"execution_count": 9,
1061+
"metadata": {},
1062+
"outputs": [
1063+
{
1064+
"name": "stdout",
1065+
"output_type": "stream",
1066+
"text": [
1067+
"1\n",
1068+
"8\n",
1069+
"-1865008399 -524833575 -1162539990 -1003374718 -1677272376 -2046456104 -689632770 -688705725\n",
1070+
"800\n"
1071+
]
1072+
}
1073+
],
1074+
"source": [
1075+
"if __name__ == '__main__':\n",
1076+
" n = int(input())\n",
1077+
" while n != 0:\n",
1078+
" p = int(input())\n",
1079+
" lis = [int(x) for x in input().split()]\n",
1080+
" bits = 0\n",
1081+
" for i in range(0, 32):\n",
1082+
" k = 0\n",
1083+
" for j in range(0, len(lis)):\n",
1084+
" if lis[j] & (1 << i):\n",
1085+
" k = k + 1\n",
1086+
" bits += k * (len(lis) - k)\n",
1087+
" print(2 * bits % 1000000007)\n",
1088+
" n = n-1"
1089+
]
1090+
},
1091+
{
1092+
"cell_type": "markdown",
1093+
"metadata": {},
1094+
"source": [
1095+
"### [Bleak Numbers](https://practice.geeksforgeeks.org/problems/bleak-numbers/0/?track=sp-bit-magic&batchId=152)\n",
1096+
"- Company Tags : SAP Labs\n",
1097+
"- Difficulty: Medium\n",
1098+
"- Marks: 4\n",
1099+
"\n",
1100+
"***Given an integer, check whether it is Bleak or not.***\n",
1101+
"\n",
1102+
"***A number ‘n’ is called Bleak if it cannot be represented as sum of a positive number x and set bit count in x, i.e., x + [countSetBits(x)](http://www.geeksforgeeks.org/count-set-bits-in-an-integer/) is not equal to n for any non-negative number x.***\n",
1103+
"\n",
1104+
"***Examples :***\n",
1105+
"\n",
1106+
"3 is not Bleak as it can be represented\n",
1107+
"as 2 + countSetBits(2).\n",
1108+
"\n",
1109+
"4 is t Bleak as it cannot be represented \n",
1110+
"as sum of a number x and countSetBits(x)\n",
1111+
"for any number x.\n",
1112+
"\n",
1113+
"***Input:***\\\n",
1114+
"The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case consists of a single line. The first line of each test case contains a single integer N to be checked for Bleak.\n",
1115+
"\n",
1116+
"***Output:***\\\n",
1117+
"Print \"1\" or \"0\" (without quotes) depending on whether the number is Bleak or not.\n",
1118+
"\n",
1119+
"***Constraints:***\\\n",
1120+
"1 <= T <= 1000\\\n",
1121+
"1 <= N <= 10000\n",
1122+
"\n",
1123+
"***Example:***\\\n",
1124+
"***Input:***\\\n",
1125+
"3\\\n",
1126+
"4\\\n",
1127+
"167\\\n",
1128+
"3\n",
1129+
"\n",
1130+
"***Output:***\\\n",
1131+
"1\\\n",
1132+
"0\\\n",
1133+
"0"
1134+
]
1135+
},
1136+
{
1137+
"cell_type": "code",
1138+
"execution_count": 9,
1139+
"metadata": {},
1140+
"outputs": [
1141+
{
1142+
"name": "stdout",
1143+
"output_type": "stream",
1144+
"text": [
1145+
"1\n",
1146+
"887\n",
1147+
"1\n"
1148+
]
1149+
}
1150+
],
1151+
"source": [
1152+
"for _ in range(int(input())):\n",
1153+
" n=int(input())\n",
1154+
" for i in range(0,n+1,2):\n",
1155+
" if (i+bin(i).count('1'))==n:\n",
1156+
" print(0)\n",
1157+
" break\n",
1158+
" else:\n",
1159+
" print(1)"
1160+
]
1161+
},
10571162
{
10581163
"cell_type": "code",
10591164
"execution_count": 20,
@@ -1116,25 +1221,46 @@
11161221
},
11171222
{
11181223
"cell_type": "code",
1119-
"execution_count": 9,
1224+
"execution_count": 2,
11201225
"metadata": {},
11211226
"outputs": [
11221227
{
11231228
"data": {
11241229
"text/plain": [
1125-
"'1100100'"
1230+
"'-0b10'"
11261231
]
11271232
},
1128-
"execution_count": 9,
1233+
"execution_count": 2,
11291234
"metadata": {},
11301235
"output_type": "execute_result"
11311236
}
11321237
],
11331238
"source": [
1134-
"a=bin(100)[2:]\n",
1239+
"a=bin(-2)\n",
11351240
"a"
11361241
]
11371242
},
1243+
{
1244+
"cell_type": "code",
1245+
"execution_count": 4,
1246+
"metadata": {},
1247+
"outputs": [
1248+
{
1249+
"ename": "ValueError",
1250+
"evalue": "invalid literal for int() with base 2: '1b10'",
1251+
"output_type": "error",
1252+
"traceback": [
1253+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
1254+
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
1255+
"\u001b[0;32m<ipython-input-4-d59bf4de1a76>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'1b10'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
1256+
"\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 2: '1b10'"
1257+
]
1258+
}
1259+
],
1260+
"source": [
1261+
"int('1b10',2)"
1262+
]
1263+
},
11381264
{
11391265
"cell_type": "code",
11401266
"execution_count": 8,

0 commit comments

Comments
 (0)