description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
There are 8 prison cells in a row, and each cell is either occupied or vacant.
Each day, whether the cell is occupied or vacant changes according to the following rules:
If a cell has two adjacent neighbors that are both occupied or both vacant, then the cell becomes occupied.
Otherwise, it becomes vacant.
(Note that because the prison is a row, the first and the last cells in the row can't have two adjacent neighbors.)
We describe the current state of the prison in the following way: cells[i] == 1 if the i-th cell is occupied, else cells[i] == 0.
Given the initial state of the prison, return the state of the prison after N days (and N such changes described above.)
Example 1:
Input: cells = [0,1,0,1,1,0,0,1], N = 7
Output: [0,0,1,1,0,0,0,0]
Explanation:
The following table summarizes the state of the prison on each day:
Day 0: [0, 1, 0, 1, 1, 0, 0, 1]
Day 1: [0, 1, 1, 0, 0, 0, 0, 0]
Day 2: [0, 0, 0, 0, 1, 1, 1, 0]
Day 3: [0, 1, 1, 0, 0, 1, 0, 0]
Day 4: [0, 0, 0, 0, 0, 1, 0, 0]
Day 5: [0, 1, 1, 1, 0, 1, 0, 0]
Day 6: [0, 0, 1, 0, 1, 1, 0, 0]
Day 7: [0, 0, 1, 1, 0, 0, 0, 0]
Example 2:
Input: cells = [1,0,0,1,0,0,1,0], N = 1000000000
Output: [0,0,1,1,1,1,1,0]
Note:
cells.length == 8
cells[i] is in {0, 1}
1 <= N <= 10^9 | class Solution:
def prisonAfterNDays(self, cells: List[int], N: int) -> List[int]:
def nextDay(cells):
ans = [0]
for i in range(1, len(cells) - 1):
ans.append(int(cells[i - 1] == cells[i + 1]))
ans.append(0)
return ans
seen = {}
forwarded = False
while N > 0:
if not forwarded:
snapshot = tuple(cells)
if snapshot in seen:
N %= seen[snapshot] - N
forwarded = True
else:
seen[snapshot] = N
if N > 0:
N -= 1
cells = nextDay(cells)
return cells | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN VAR ASSIGN VAR DICT ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR VAR VAR |
There are 8 prison cells in a row, and each cell is either occupied or vacant.
Each day, whether the cell is occupied or vacant changes according to the following rules:
If a cell has two adjacent neighbors that are both occupied or both vacant, then the cell becomes occupied.
Otherwise, it becomes vacant.
(Note that because the prison is a row, the first and the last cells in the row can't have two adjacent neighbors.)
We describe the current state of the prison in the following way: cells[i] == 1 if the i-th cell is occupied, else cells[i] == 0.
Given the initial state of the prison, return the state of the prison after N days (and N such changes described above.)
Example 1:
Input: cells = [0,1,0,1,1,0,0,1], N = 7
Output: [0,0,1,1,0,0,0,0]
Explanation:
The following table summarizes the state of the prison on each day:
Day 0: [0, 1, 0, 1, 1, 0, 0, 1]
Day 1: [0, 1, 1, 0, 0, 0, 0, 0]
Day 2: [0, 0, 0, 0, 1, 1, 1, 0]
Day 3: [0, 1, 1, 0, 0, 1, 0, 0]
Day 4: [0, 0, 0, 0, 0, 1, 0, 0]
Day 5: [0, 1, 1, 1, 0, 1, 0, 0]
Day 6: [0, 0, 1, 0, 1, 1, 0, 0]
Day 7: [0, 0, 1, 1, 0, 0, 0, 0]
Example 2:
Input: cells = [1,0,0,1,0,0,1,0], N = 1000000000
Output: [0,0,1,1,1,1,1,0]
Note:
cells.length == 8
cells[i] is in {0, 1}
1 <= N <= 10^9 | class Solution:
def prisonAfterNDays(self, cells, N):
seen = {}
while N:
seen.setdefault(str(cells), N)
print(seen)
N -= 1
cells = [0] + [(cells[i - 1] ^ cells[i + 1] ^ 1) for i in range(1, 7)] + [0]
if str(cells) in seen:
N %= seen[str(cells)] - N
return cells | CLASS_DEF FUNC_DEF ASSIGN VAR DICT WHILE VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER NUMBER LIST NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR RETURN VAR |
There are 8 prison cells in a row, and each cell is either occupied or vacant.
Each day, whether the cell is occupied or vacant changes according to the following rules:
If a cell has two adjacent neighbors that are both occupied or both vacant, then the cell becomes occupied.
Otherwise, it becomes vacant.
(Note that because the prison is a row, the first and the last cells in the row can't have two adjacent neighbors.)
We describe the current state of the prison in the following way: cells[i] == 1 if the i-th cell is occupied, else cells[i] == 0.
Given the initial state of the prison, return the state of the prison after N days (and N such changes described above.)
Example 1:
Input: cells = [0,1,0,1,1,0,0,1], N = 7
Output: [0,0,1,1,0,0,0,0]
Explanation:
The following table summarizes the state of the prison on each day:
Day 0: [0, 1, 0, 1, 1, 0, 0, 1]
Day 1: [0, 1, 1, 0, 0, 0, 0, 0]
Day 2: [0, 0, 0, 0, 1, 1, 1, 0]
Day 3: [0, 1, 1, 0, 0, 1, 0, 0]
Day 4: [0, 0, 0, 0, 0, 1, 0, 0]
Day 5: [0, 1, 1, 1, 0, 1, 0, 0]
Day 6: [0, 0, 1, 0, 1, 1, 0, 0]
Day 7: [0, 0, 1, 1, 0, 0, 0, 0]
Example 2:
Input: cells = [1,0,0,1,0,0,1,0], N = 1000000000
Output: [0,0,1,1,1,1,1,0]
Note:
cells.length == 8
cells[i] is in {0, 1}
1 <= N <= 10^9 | class Solution:
def prisonAfterNDays(self, cells: List[int], N: int) -> List[int]:
seen = dict()
is_fast_forwarded = False
state_bitmap = 0
for cell in cells:
state_bitmap <<= 1
state_bitmap = state_bitmap | cell
while N > 0:
if not is_fast_forwarded:
if state_bitmap in seen:
N %= seen[state_bitmap] - N
is_fast_forwarded = True
else:
seen[state_bitmap] = N
if N > 0:
N -= 1
state_bitmap = self.nextDay(state_bitmap)
ret = []
for i in range(len(cells)):
ret.append(state_bitmap & 1)
state_bitmap = state_bitmap >> 1
return reversed(ret)
def nextDay(self, state_bitmap: int):
state_bitmap = ~(state_bitmap << 1) ^ state_bitmap >> 1
state_bitmap = state_bitmap & 126
return state_bitmap | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE VAR NUMBER IF VAR IF VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
There are 8 prison cells in a row, and each cell is either occupied or vacant.
Each day, whether the cell is occupied or vacant changes according to the following rules:
If a cell has two adjacent neighbors that are both occupied or both vacant, then the cell becomes occupied.
Otherwise, it becomes vacant.
(Note that because the prison is a row, the first and the last cells in the row can't have two adjacent neighbors.)
We describe the current state of the prison in the following way: cells[i] == 1 if the i-th cell is occupied, else cells[i] == 0.
Given the initial state of the prison, return the state of the prison after N days (and N such changes described above.)
Example 1:
Input: cells = [0,1,0,1,1,0,0,1], N = 7
Output: [0,0,1,1,0,0,0,0]
Explanation:
The following table summarizes the state of the prison on each day:
Day 0: [0, 1, 0, 1, 1, 0, 0, 1]
Day 1: [0, 1, 1, 0, 0, 0, 0, 0]
Day 2: [0, 0, 0, 0, 1, 1, 1, 0]
Day 3: [0, 1, 1, 0, 0, 1, 0, 0]
Day 4: [0, 0, 0, 0, 0, 1, 0, 0]
Day 5: [0, 1, 1, 1, 0, 1, 0, 0]
Day 6: [0, 0, 1, 0, 1, 1, 0, 0]
Day 7: [0, 0, 1, 1, 0, 0, 0, 0]
Example 2:
Input: cells = [1,0,0,1,0,0,1,0], N = 1000000000
Output: [0,0,1,1,1,1,1,0]
Note:
cells.length == 8
cells[i] is in {0, 1}
1 <= N <= 10^9 | class Solution:
def prisonAfterNDays(self, cells: List[int], N: int) -> List[int]:
visited = {str(cells): N}
cycle = 0
hascycle = False
def get_next(cells) -> List[int]:
tmp = [0] + [(cells[i - 1] ^ cells[i + 1] ^ 1) for i in range(1, 7)] + [0]
return tmp
while N > 0:
visited[str(cells)] = N
N -= 1
cells = get_next(cells.copy())
if str(cells) in visited:
hascycle = True
cycle = visited[str(cells)] - N
break
if hascycle:
N = N % cycle
while N > 0:
N -= 1
cells = get_next(cells)
return cells | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP BIN_OP LIST NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER NUMBER LIST NUMBER RETURN VAR VAR VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR IF VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR VAR VAR |
There are 8 prison cells in a row, and each cell is either occupied or vacant.
Each day, whether the cell is occupied or vacant changes according to the following rules:
If a cell has two adjacent neighbors that are both occupied or both vacant, then the cell becomes occupied.
Otherwise, it becomes vacant.
(Note that because the prison is a row, the first and the last cells in the row can't have two adjacent neighbors.)
We describe the current state of the prison in the following way: cells[i] == 1 if the i-th cell is occupied, else cells[i] == 0.
Given the initial state of the prison, return the state of the prison after N days (and N such changes described above.)
Example 1:
Input: cells = [0,1,0,1,1,0,0,1], N = 7
Output: [0,0,1,1,0,0,0,0]
Explanation:
The following table summarizes the state of the prison on each day:
Day 0: [0, 1, 0, 1, 1, 0, 0, 1]
Day 1: [0, 1, 1, 0, 0, 0, 0, 0]
Day 2: [0, 0, 0, 0, 1, 1, 1, 0]
Day 3: [0, 1, 1, 0, 0, 1, 0, 0]
Day 4: [0, 0, 0, 0, 0, 1, 0, 0]
Day 5: [0, 1, 1, 1, 0, 1, 0, 0]
Day 6: [0, 0, 1, 0, 1, 1, 0, 0]
Day 7: [0, 0, 1, 1, 0, 0, 0, 0]
Example 2:
Input: cells = [1,0,0,1,0,0,1,0], N = 1000000000
Output: [0,0,1,1,1,1,1,0]
Note:
cells.length == 8
cells[i] is in {0, 1}
1 <= N <= 10^9 | class Solution:
def prisonAfterNDays(self, cells: List[int], N: int) -> List[int]:
prison_size = len(cells)
seen = {}
seen[str(cells)] = 0
history = [cells]
for d in range(1, N + 1):
ans = [0] * prison_size
for i in range(1, 7):
ans[i] = 1 if cells[i - 1] == cells[i + 1] else 0
if str(ans) in seen:
cycle_length = d - seen[str(ans)]
return history[seen[str(ans)] + (N - d) % cycle_length]
else:
history.append(ans)
seen[str(ans)] = d
cells = ans
return cells | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR RETURN VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR RETURN VAR VAR VAR |
There are 8 prison cells in a row, and each cell is either occupied or vacant.
Each day, whether the cell is occupied or vacant changes according to the following rules:
If a cell has two adjacent neighbors that are both occupied or both vacant, then the cell becomes occupied.
Otherwise, it becomes vacant.
(Note that because the prison is a row, the first and the last cells in the row can't have two adjacent neighbors.)
We describe the current state of the prison in the following way: cells[i] == 1 if the i-th cell is occupied, else cells[i] == 0.
Given the initial state of the prison, return the state of the prison after N days (and N such changes described above.)
Example 1:
Input: cells = [0,1,0,1,1,0,0,1], N = 7
Output: [0,0,1,1,0,0,0,0]
Explanation:
The following table summarizes the state of the prison on each day:
Day 0: [0, 1, 0, 1, 1, 0, 0, 1]
Day 1: [0, 1, 1, 0, 0, 0, 0, 0]
Day 2: [0, 0, 0, 0, 1, 1, 1, 0]
Day 3: [0, 1, 1, 0, 0, 1, 0, 0]
Day 4: [0, 0, 0, 0, 0, 1, 0, 0]
Day 5: [0, 1, 1, 1, 0, 1, 0, 0]
Day 6: [0, 0, 1, 0, 1, 1, 0, 0]
Day 7: [0, 0, 1, 1, 0, 0, 0, 0]
Example 2:
Input: cells = [1,0,0,1,0,0,1,0], N = 1000000000
Output: [0,0,1,1,1,1,1,0]
Note:
cells.length == 8
cells[i] is in {0, 1}
1 <= N <= 10^9 | class Solution:
def prisonAfterNDays(self, cells: List[int], N: int) -> List[int]:
def next(state):
return tuple(
(
1
if i > 0 and i < len(state) - 1 and state[i - 1] == state[i + 1]
else 0
)
for i in range(len(state))
)
seen = {}
state = tuple(cells)
i = 0
remaining = 0
while i < N:
if state in seen:
cycle = i - seen[state]
remaining = (N - i) % cycle
break
seen[state] = i
state = next(state)
i += 1
while remaining > 0:
state = next(state)
remaining -= 1
return state | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR VAR |
There are 8 prison cells in a row, and each cell is either occupied or vacant.
Each day, whether the cell is occupied or vacant changes according to the following rules:
If a cell has two adjacent neighbors that are both occupied or both vacant, then the cell becomes occupied.
Otherwise, it becomes vacant.
(Note that because the prison is a row, the first and the last cells in the row can't have two adjacent neighbors.)
We describe the current state of the prison in the following way: cells[i] == 1 if the i-th cell is occupied, else cells[i] == 0.
Given the initial state of the prison, return the state of the prison after N days (and N such changes described above.)
Example 1:
Input: cells = [0,1,0,1,1,0,0,1], N = 7
Output: [0,0,1,1,0,0,0,0]
Explanation:
The following table summarizes the state of the prison on each day:
Day 0: [0, 1, 0, 1, 1, 0, 0, 1]
Day 1: [0, 1, 1, 0, 0, 0, 0, 0]
Day 2: [0, 0, 0, 0, 1, 1, 1, 0]
Day 3: [0, 1, 1, 0, 0, 1, 0, 0]
Day 4: [0, 0, 0, 0, 0, 1, 0, 0]
Day 5: [0, 1, 1, 1, 0, 1, 0, 0]
Day 6: [0, 0, 1, 0, 1, 1, 0, 0]
Day 7: [0, 0, 1, 1, 0, 0, 0, 0]
Example 2:
Input: cells = [1,0,0,1,0,0,1,0], N = 1000000000
Output: [0,0,1,1,1,1,1,0]
Note:
cells.length == 8
cells[i] is in {0, 1}
1 <= N <= 10^9 | class Solution:
def prisonAfterNDays(self, cells: List[int], N: int) -> List[int]:
for i in range((N - 1) % 14 + 1):
cells = [0] + [(cells[i - 1] ^ cells[i + 1] ^ 1) for i in range(1, 7)] + [0]
return cells | CLASS_DEF FUNC_DEF VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP LIST NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER NUMBER LIST NUMBER RETURN VAR VAR VAR |
There are 8 prison cells in a row, and each cell is either occupied or vacant.
Each day, whether the cell is occupied or vacant changes according to the following rules:
If a cell has two adjacent neighbors that are both occupied or both vacant, then the cell becomes occupied.
Otherwise, it becomes vacant.
(Note that because the prison is a row, the first and the last cells in the row can't have two adjacent neighbors.)
We describe the current state of the prison in the following way: cells[i] == 1 if the i-th cell is occupied, else cells[i] == 0.
Given the initial state of the prison, return the state of the prison after N days (and N such changes described above.)
Example 1:
Input: cells = [0,1,0,1,1,0,0,1], N = 7
Output: [0,0,1,1,0,0,0,0]
Explanation:
The following table summarizes the state of the prison on each day:
Day 0: [0, 1, 0, 1, 1, 0, 0, 1]
Day 1: [0, 1, 1, 0, 0, 0, 0, 0]
Day 2: [0, 0, 0, 0, 1, 1, 1, 0]
Day 3: [0, 1, 1, 0, 0, 1, 0, 0]
Day 4: [0, 0, 0, 0, 0, 1, 0, 0]
Day 5: [0, 1, 1, 1, 0, 1, 0, 0]
Day 6: [0, 0, 1, 0, 1, 1, 0, 0]
Day 7: [0, 0, 1, 1, 0, 0, 0, 0]
Example 2:
Input: cells = [1,0,0,1,0,0,1,0], N = 1000000000
Output: [0,0,1,1,1,1,1,0]
Note:
cells.length == 8
cells[i] is in {0, 1}
1 <= N <= 10^9 | class Solution:
def prisonAfterNDays(self, cells: List[int], N: int) -> List[int]:
def transform(cells):
nxtCells = [0] * len(cells)
for i in range(1, len(cells) - 1):
nxtCells[i] = 1 if cells[i - 1] == cells[i + 1] else 0
return nxtCells
day = 0
while N > 0:
cells = transform(cells)
day += 1
N -= 1
if day == 1:
origin = [i for i in cells]
elif cells == origin:
N %= day - 1
return cells | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER RETURN VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR |
There are 8 prison cells in a row, and each cell is either occupied or vacant.
Each day, whether the cell is occupied or vacant changes according to the following rules:
If a cell has two adjacent neighbors that are both occupied or both vacant, then the cell becomes occupied.
Otherwise, it becomes vacant.
(Note that because the prison is a row, the first and the last cells in the row can't have two adjacent neighbors.)
We describe the current state of the prison in the following way: cells[i] == 1 if the i-th cell is occupied, else cells[i] == 0.
Given the initial state of the prison, return the state of the prison after N days (and N such changes described above.)
Example 1:
Input: cells = [0,1,0,1,1,0,0,1], N = 7
Output: [0,0,1,1,0,0,0,0]
Explanation:
The following table summarizes the state of the prison on each day:
Day 0: [0, 1, 0, 1, 1, 0, 0, 1]
Day 1: [0, 1, 1, 0, 0, 0, 0, 0]
Day 2: [0, 0, 0, 0, 1, 1, 1, 0]
Day 3: [0, 1, 1, 0, 0, 1, 0, 0]
Day 4: [0, 0, 0, 0, 0, 1, 0, 0]
Day 5: [0, 1, 1, 1, 0, 1, 0, 0]
Day 6: [0, 0, 1, 0, 1, 1, 0, 0]
Day 7: [0, 0, 1, 1, 0, 0, 0, 0]
Example 2:
Input: cells = [1,0,0,1,0,0,1,0], N = 1000000000
Output: [0,0,1,1,1,1,1,0]
Note:
cells.length == 8
cells[i] is in {0, 1}
1 <= N <= 10^9 | class Solution:
def prisonAfterNDays(self, cells: List[int], N: int) -> List[int]:
if not cells:
return cells
day1 = self.nextDay(cells)
cells = day1.copy()
count = 0
N -= 1
while N > 0:
each_day = self.nextDay(cells)
count += 1
N -= 1
if each_day == day1:
N = N % count
cells = each_day
return cells
def nextDay(self, cells):
ret = [0]
for i in range(1, len(cells) - 1):
ret.append(int(cells[i - 1] == cells[i + 1]))
ret.append(0)
return ret
return cells | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN VAR RETURN VAR |
There are 8 prison cells in a row, and each cell is either occupied or vacant.
Each day, whether the cell is occupied or vacant changes according to the following rules:
If a cell has two adjacent neighbors that are both occupied or both vacant, then the cell becomes occupied.
Otherwise, it becomes vacant.
(Note that because the prison is a row, the first and the last cells in the row can't have two adjacent neighbors.)
We describe the current state of the prison in the following way: cells[i] == 1 if the i-th cell is occupied, else cells[i] == 0.
Given the initial state of the prison, return the state of the prison after N days (and N such changes described above.)
Example 1:
Input: cells = [0,1,0,1,1,0,0,1], N = 7
Output: [0,0,1,1,0,0,0,0]
Explanation:
The following table summarizes the state of the prison on each day:
Day 0: [0, 1, 0, 1, 1, 0, 0, 1]
Day 1: [0, 1, 1, 0, 0, 0, 0, 0]
Day 2: [0, 0, 0, 0, 1, 1, 1, 0]
Day 3: [0, 1, 1, 0, 0, 1, 0, 0]
Day 4: [0, 0, 0, 0, 0, 1, 0, 0]
Day 5: [0, 1, 1, 1, 0, 1, 0, 0]
Day 6: [0, 0, 1, 0, 1, 1, 0, 0]
Day 7: [0, 0, 1, 1, 0, 0, 0, 0]
Example 2:
Input: cells = [1,0,0,1,0,0,1,0], N = 1000000000
Output: [0,0,1,1,1,1,1,0]
Note:
cells.length == 8
cells[i] is in {0, 1}
1 <= N <= 10^9 | class Solution:
def prisonAfterNDays(self, cells: List[int], N: int) -> List[int]:
cell = 0
for c in cells:
cell = cell << 1 | c
seendict = {}
index = 0
while True:
if cell in seendict:
if (N - index) % (index - seendict[cell]) == 0:
return [int(x) for x in "{0:08b}".format(cell)]
seendict[cell] = index
not_cell = cell ^ 255
cell = cell << 1 & cell >> 1 | not_cell << 1 & not_cell >> 1
index += 1 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR DICT ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR IF BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_CALL STRING VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR VAR |
You are given an array of $n$ integers $a_1, a_2, \ldots, a_n$.
You will perform $q$ operations. In the $i$-th operation, you have a symbol $s_i$ which is either "<" or ">" and a number $x_i$.
You make a new array $b$ such that $b_j = -a_j$ if $a_j s_i x_i$ and $b_j = a_j$ otherwise (i.e. if $s_i$ is '>', then all $a_j > x_i$ will be flipped). After doing all these replacements, $a$ is set to be $b$.
You want to know what your final array looks like after all operations.
-----Input-----
The first line contains two integers $n,q$ ($1 \leq n,q \leq 10^5$) — the number of integers and the number of queries.
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^5 \leq a_i \leq 10^5$) — the numbers.
Each of the next $q$ lines contains a character and an integer $s_i, x_i$. ($s_i \in \{<, >\}, -10^5 \leq x_i \leq 10^5$) – the queries.
-----Output-----
Print $n$ integers $c_1, c_2, \ldots, c_n$ representing the array after all operations.
-----Examples-----
Input
11 3
-5 -4 -3 -2 -1 0 1 2 3 4 5
> 2
> -4
< 5
Output
5 4 -3 -2 -1 0 1 2 -3 4 5
Input
5 5
0 1 -2 -1 2
< -2
< -1
< 0
< 1
< 2
Output
0 -1 2 -1 2
-----Note-----
In the first example, the array goes through the following changes: Initial: $[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]$ $> 2$: $[-5, -4, -3, -2, -1, 0, 1, 2, -3, -4, -5]$ $> -4$: $[-5, -4, 3, 2, 1, 0, -1, -2, 3, -4, -5]$ $< 5$: $[5, 4, -3, -2, -1, 0, 1, 2, -3, 4, 5]$ | import sys
N, Q = map(int, input().split())
A = list(map(int, input().split()))
Sign = [0] * (3 + 10**5)
SX = [tuple(sys.stdin.readline().split()) for _ in range(Q)]
mini = 3 + 10**5
Sign = [0] * (3 + 10**5)
keep = 1
for s, x in SX[::-1]:
x = int(x)
ax = abs(x) + int((s == ">") ^ (x < 0))
sin = 1 if s == "<" else -1
if mini > ax:
t = sin * keep
for i in range(mini - 1, ax - 1, -1):
Sign[i] = t
mini = ax
keep *= (2 * (s == ">") - 1) * (2 * (0 < x) - 1)
Ans = [None] * N
for i, a in enumerate(A):
s = Sign[abs(a)]
if s == 0:
s = (2 * (a > 0) - 1) * keep
Ans[i] = s * abs(a)
print(*Ans) | IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR STRING VAR NUMBER ASSIGN VAR VAR STRING NUMBER NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR STRING NUMBER BIN_OP BIN_OP NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NONE VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER VAR ASSIGN VAR VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given an array of $n$ integers $a_1, a_2, \ldots, a_n$.
You will perform $q$ operations. In the $i$-th operation, you have a symbol $s_i$ which is either "<" or ">" and a number $x_i$.
You make a new array $b$ such that $b_j = -a_j$ if $a_j s_i x_i$ and $b_j = a_j$ otherwise (i.e. if $s_i$ is '>', then all $a_j > x_i$ will be flipped). After doing all these replacements, $a$ is set to be $b$.
You want to know what your final array looks like after all operations.
-----Input-----
The first line contains two integers $n,q$ ($1 \leq n,q \leq 10^5$) — the number of integers and the number of queries.
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^5 \leq a_i \leq 10^5$) — the numbers.
Each of the next $q$ lines contains a character and an integer $s_i, x_i$. ($s_i \in \{<, >\}, -10^5 \leq x_i \leq 10^5$) – the queries.
-----Output-----
Print $n$ integers $c_1, c_2, \ldots, c_n$ representing the array after all operations.
-----Examples-----
Input
11 3
-5 -4 -3 -2 -1 0 1 2 3 4 5
> 2
> -4
< 5
Output
5 4 -3 -2 -1 0 1 2 -3 4 5
Input
5 5
0 1 -2 -1 2
< -2
< -1
< 0
< 1
< 2
Output
0 -1 2 -1 2
-----Note-----
In the first example, the array goes through the following changes: Initial: $[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]$ $> 2$: $[-5, -4, -3, -2, -1, 0, 1, 2, -3, -4, -5]$ $> -4$: $[-5, -4, 3, 2, 1, 0, -1, -2, 3, -4, -5]$ $< 5$: $[5, 4, -3, -2, -1, 0, 1, 2, -3, 4, 5]$ | n, q = map(int, input().split())
a = list(map(int, input().split()))
swaps = [0] * q
for i in range(q):
b = input().split()
b[1] = int(b[1])
out = 1 if b[0] == "<" and b[1] <= 0 or b[0] == ">" and b[1] >= 0 else 0
split = b[1] + 0.5 if b[0] == ">" else b[1] - 0.5
sign = 1 if split > 0 else -1
split = abs(split)
swaps[i] = split, sign, out
sml = 10**5 + 0.5
zeros = 0
for i in range(q):
sml = min(swaps[i][0], sml)
zeros += 1 - swaps[i][2]
zeros %= 2
arr = [0] * 100001
big = 100000.5
flips = 1
for i in range(q):
if swaps[-1 - i][0] < big:
if swaps[-1 - i][2] == 1:
for j in range(int(swaps[-1 - i][0] + 0.5), int(big + 0.5)):
arr[j] = -swaps[-i - 1][1] * flips
else:
for j in range(int(swaps[-1 - i][0] + 0.5), int(big + 0.5)):
arr[j] = swaps[-i - 1][1] * flips
big = swaps[-1 - i][0]
if swaps[-1 - i][2] == 0:
flips = -flips
def func(k):
if abs(k) < sml:
return k if zeros == 0 else -k
else:
return arr[abs(k)] * abs(k)
print(" ".join([str(func(guy)) for guy in a])) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER STRING VAR NUMBER NUMBER VAR NUMBER STRING VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER STRING BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP NUMBER VAR NUMBER VAR IF VAR BIN_OP NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR BIN_OP NUMBER VAR NUMBER IF VAR BIN_OP NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN VAR NUMBER VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR |
The Chef has prepared the appetizers in the shapes of letters to spell a special message for the guests. There are n appetizers numbered from 0 to n-1 such that if the appetizers are arrayed in this order, they will display the message. The Chef plans to display them in this order on a table that can be viewed by all guests as they enter. The appetizers will only be served once all guests are seated.
The appetizers are not necessarily finished in the same order as they are numbered. So, when an appetizer is finished the Chef will write the number on a piece of paper and place it beside the appetizer on a counter between the kitchen and the restaurant. A server will retrieve this appetizer and place it in the proper location according to the number written beside it.
The Chef has a penchant for binary numbers. The number of appetizers created is a power of 2, say n = 2^{k}. Furthermore, he has written the number of the appetizer in binary with exactly k bits. That is, binary numbers with fewer than k bits are padded on the left with zeros so they are written with exactly k bits.
Unfortunately, this has unforseen complications. A binary number still "looks" binary when it is written upside down. For example, the binary number "0101" looks like "1010" when read upside down and the binary number "110" looks like "011" (the Chef uses simple vertical lines to denote a 1 bit). The Chef didn't realize that the servers would read the numbers upside down so he doesn't rotate the paper when he places it on the counter. Thus, when the server picks up an appetizer they place it the location indexed by the binary number when it is read upside down.
You are given the message the chef intended to display and you are to display the message that will be displayed after the servers move all appetizers to their locations based on the binary numbers they read.
------ Input ------
The first line consists of a single integer T ≤ 25 indicating the number of test cases to follow. Each test case consists of a single line beginning with an integer 1 ≤ k ≤ 16 followed by a string of precisely 2^{k} characters. The integer and the string are separated by a single space. The string has no spaces and is composed only of lower case letters from a to z.
------ Output ------
For each test case you are to output the scrambled message on a single line.
----- Sample Input 1 ------
2
2 chef
4 enjoyourapplepie
----- Sample Output 1 ------
cehf
eayejpuinpopolre | def convert(x, k):
binary = ""
while x != 0:
d = x % 2
binary += str(d)
x //= 2
while 1:
if len(binary) != k:
binary += "0"
else:
break
decimal = 0
for i in range(len(binary)):
if binary[i] == "1":
decimal += 2 ** (k - i - 1)
return decimal
def fun(s):
s = s.split(" ")
k = int(s[0])
s = s[1]
ans = [""] * len(s)
for i in range(len(s)):
ans[i] = s[convert(i, k)]
x = ""
for i in ans:
x += i
return x
t = int(input(""))
for xxx in range(t):
s = input("")
print(fun(s)) | FUNC_DEF ASSIGN VAR STRING WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER WHILE NUMBER IF FUNC_CALL VAR VAR VAR VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST STRING FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR STRING FOR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
The Chef has prepared the appetizers in the shapes of letters to spell a special message for the guests. There are n appetizers numbered from 0 to n-1 such that if the appetizers are arrayed in this order, they will display the message. The Chef plans to display them in this order on a table that can be viewed by all guests as they enter. The appetizers will only be served once all guests are seated.
The appetizers are not necessarily finished in the same order as they are numbered. So, when an appetizer is finished the Chef will write the number on a piece of paper and place it beside the appetizer on a counter between the kitchen and the restaurant. A server will retrieve this appetizer and place it in the proper location according to the number written beside it.
The Chef has a penchant for binary numbers. The number of appetizers created is a power of 2, say n = 2^{k}. Furthermore, he has written the number of the appetizer in binary with exactly k bits. That is, binary numbers with fewer than k bits are padded on the left with zeros so they are written with exactly k bits.
Unfortunately, this has unforseen complications. A binary number still "looks" binary when it is written upside down. For example, the binary number "0101" looks like "1010" when read upside down and the binary number "110" looks like "011" (the Chef uses simple vertical lines to denote a 1 bit). The Chef didn't realize that the servers would read the numbers upside down so he doesn't rotate the paper when he places it on the counter. Thus, when the server picks up an appetizer they place it the location indexed by the binary number when it is read upside down.
You are given the message the chef intended to display and you are to display the message that will be displayed after the servers move all appetizers to their locations based on the binary numbers they read.
------ Input ------
The first line consists of a single integer T ≤ 25 indicating the number of test cases to follow. Each test case consists of a single line beginning with an integer 1 ≤ k ≤ 16 followed by a string of precisely 2^{k} characters. The integer and the string are separated by a single space. The string has no spaces and is composed only of lower case letters from a to z.
------ Output ------
For each test case you are to output the scrambled message on a single line.
----- Sample Input 1 ------
2
2 chef
4 enjoyourapplepie
----- Sample Output 1 ------
cehf
eayejpuinpopolre | def decimal_to_binary(n):
if n == 0:
list1 = []
list1.append(0)
return list1
list1 = []
while n:
list1.append(n % 2)
n = n // 2
list1.reverse()
return list1
def binary_to_decimal(list1, k):
ans = 0
size = len(list1) - 1
for i in range(size, 0 - 1, -1):
if list1[i] == 1:
ans += 2**k
k -= 1
return ans
t = int(input())
for _ in range(t):
line1 = input()
k, st = list(line1.split())
k = int(k)
st = list(st)
track = set()
for i in range(2**k):
if i in track:
continue
rev_binary = decimal_to_binary(i)
i1 = binary_to_decimal(rev_binary, k - 1)
track.add(i1)
temp = st[i]
st[i] = st[i1]
st[i1] = temp
for value in st:
print(value, sep="", end="")
print() | FUNC_DEF IF VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER RETURN VAR ASSIGN VAR LIST WHILE VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING STRING EXPR FUNC_CALL VAR |
The Chef has prepared the appetizers in the shapes of letters to spell a special message for the guests. There are n appetizers numbered from 0 to n-1 such that if the appetizers are arrayed in this order, they will display the message. The Chef plans to display them in this order on a table that can be viewed by all guests as they enter. The appetizers will only be served once all guests are seated.
The appetizers are not necessarily finished in the same order as they are numbered. So, when an appetizer is finished the Chef will write the number on a piece of paper and place it beside the appetizer on a counter between the kitchen and the restaurant. A server will retrieve this appetizer and place it in the proper location according to the number written beside it.
The Chef has a penchant for binary numbers. The number of appetizers created is a power of 2, say n = 2^{k}. Furthermore, he has written the number of the appetizer in binary with exactly k bits. That is, binary numbers with fewer than k bits are padded on the left with zeros so they are written with exactly k bits.
Unfortunately, this has unforseen complications. A binary number still "looks" binary when it is written upside down. For example, the binary number "0101" looks like "1010" when read upside down and the binary number "110" looks like "011" (the Chef uses simple vertical lines to denote a 1 bit). The Chef didn't realize that the servers would read the numbers upside down so he doesn't rotate the paper when he places it on the counter. Thus, when the server picks up an appetizer they place it the location indexed by the binary number when it is read upside down.
You are given the message the chef intended to display and you are to display the message that will be displayed after the servers move all appetizers to their locations based on the binary numbers they read.
------ Input ------
The first line consists of a single integer T ≤ 25 indicating the number of test cases to follow. Each test case consists of a single line beginning with an integer 1 ≤ k ≤ 16 followed by a string of precisely 2^{k} characters. The integer and the string are separated by a single space. The string has no spaces and is composed only of lower case letters from a to z.
------ Output ------
For each test case you are to output the scrambled message on a single line.
----- Sample Input 1 ------
2
2 chef
4 enjoyourapplepie
----- Sample Output 1 ------
cehf
eayejpuinpopolre | for i in range(int(input())):
a, b = map(str, input().split())
a = int(a)
b = list(b)
c = [""] * (1 << a)
for ii in range(1 << a):
ss = int(str("{0:0" + str(a) + "b}").format(ii), 2)
s = int(str("{0:0" + str(a) + "b}").format(ii)[::-1], 2)
c[s] = b[ss]
print(str("".join(c))) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST STRING BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR BIN_OP BIN_OP STRING FUNC_CALL VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR BIN_OP BIN_OP STRING FUNC_CALL VAR VAR STRING VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR |
The Chef has prepared the appetizers in the shapes of letters to spell a special message for the guests. There are n appetizers numbered from 0 to n-1 such that if the appetizers are arrayed in this order, they will display the message. The Chef plans to display them in this order on a table that can be viewed by all guests as they enter. The appetizers will only be served once all guests are seated.
The appetizers are not necessarily finished in the same order as they are numbered. So, when an appetizer is finished the Chef will write the number on a piece of paper and place it beside the appetizer on a counter between the kitchen and the restaurant. A server will retrieve this appetizer and place it in the proper location according to the number written beside it.
The Chef has a penchant for binary numbers. The number of appetizers created is a power of 2, say n = 2^{k}. Furthermore, he has written the number of the appetizer in binary with exactly k bits. That is, binary numbers with fewer than k bits are padded on the left with zeros so they are written with exactly k bits.
Unfortunately, this has unforseen complications. A binary number still "looks" binary when it is written upside down. For example, the binary number "0101" looks like "1010" when read upside down and the binary number "110" looks like "011" (the Chef uses simple vertical lines to denote a 1 bit). The Chef didn't realize that the servers would read the numbers upside down so he doesn't rotate the paper when he places it on the counter. Thus, when the server picks up an appetizer they place it the location indexed by the binary number when it is read upside down.
You are given the message the chef intended to display and you are to display the message that will be displayed after the servers move all appetizers to their locations based on the binary numbers they read.
------ Input ------
The first line consists of a single integer T ≤ 25 indicating the number of test cases to follow. Each test case consists of a single line beginning with an integer 1 ≤ k ≤ 16 followed by a string of precisely 2^{k} characters. The integer and the string are separated by a single space. The string has no spaces and is composed only of lower case letters from a to z.
------ Output ------
For each test case you are to output the scrambled message on a single line.
----- Sample Input 1 ------
2
2 chef
4 enjoyourapplepie
----- Sample Output 1 ------
cehf
eayejpuinpopolre | def reversebit(n, length):
a = []
for i in range(length):
a.append(n % 2)
n = n // 2
s = 0
for i in range(length):
s += a[i] * 2 ** (length - 1 - i)
return s
for _ in range(int(input())):
n, s = input().split()
n = int(n)
b = []
for i in range(2**n):
j = reversebit(i, n)
b.append(s[j])
print("".join(b)) | FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
The Chef has prepared the appetizers in the shapes of letters to spell a special message for the guests. There are n appetizers numbered from 0 to n-1 such that if the appetizers are arrayed in this order, they will display the message. The Chef plans to display them in this order on a table that can be viewed by all guests as they enter. The appetizers will only be served once all guests are seated.
The appetizers are not necessarily finished in the same order as they are numbered. So, when an appetizer is finished the Chef will write the number on a piece of paper and place it beside the appetizer on a counter between the kitchen and the restaurant. A server will retrieve this appetizer and place it in the proper location according to the number written beside it.
The Chef has a penchant for binary numbers. The number of appetizers created is a power of 2, say n = 2^{k}. Furthermore, he has written the number of the appetizer in binary with exactly k bits. That is, binary numbers with fewer than k bits are padded on the left with zeros so they are written with exactly k bits.
Unfortunately, this has unforseen complications. A binary number still "looks" binary when it is written upside down. For example, the binary number "0101" looks like "1010" when read upside down and the binary number "110" looks like "011" (the Chef uses simple vertical lines to denote a 1 bit). The Chef didn't realize that the servers would read the numbers upside down so he doesn't rotate the paper when he places it on the counter. Thus, when the server picks up an appetizer they place it the location indexed by the binary number when it is read upside down.
You are given the message the chef intended to display and you are to display the message that will be displayed after the servers move all appetizers to their locations based on the binary numbers they read.
------ Input ------
The first line consists of a single integer T ≤ 25 indicating the number of test cases to follow. Each test case consists of a single line beginning with an integer 1 ≤ k ≤ 16 followed by a string of precisely 2^{k} characters. The integer and the string are separated by a single space. The string has no spaces and is composed only of lower case letters from a to z.
------ Output ------
For each test case you are to output the scrambled message on a single line.
----- Sample Input 1 ------
2
2 chef
4 enjoyourapplepie
----- Sample Output 1 ------
cehf
eayejpuinpopolre | def transformIndex(k, index):
i = k
new_index = 0
while i > 0:
res = index % 2
index = index // 2
new_index = new_index << 1 | res
i -= 1
return new_index
def solution(k, message):
k = int(k)
ans = ""
for i in range(len(message)):
ans += message[transformIndex(k, i)]
return ans
n = int(input())
tests = []
for i in range(n):
tests.append(input().split(" "))
for test in tests:
print(solution(int(test[0]), test[1])) | FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER |
The Chef has prepared the appetizers in the shapes of letters to spell a special message for the guests. There are n appetizers numbered from 0 to n-1 such that if the appetizers are arrayed in this order, they will display the message. The Chef plans to display them in this order on a table that can be viewed by all guests as they enter. The appetizers will only be served once all guests are seated.
The appetizers are not necessarily finished in the same order as they are numbered. So, when an appetizer is finished the Chef will write the number on a piece of paper and place it beside the appetizer on a counter between the kitchen and the restaurant. A server will retrieve this appetizer and place it in the proper location according to the number written beside it.
The Chef has a penchant for binary numbers. The number of appetizers created is a power of 2, say n = 2^{k}. Furthermore, he has written the number of the appetizer in binary with exactly k bits. That is, binary numbers with fewer than k bits are padded on the left with zeros so they are written with exactly k bits.
Unfortunately, this has unforseen complications. A binary number still "looks" binary when it is written upside down. For example, the binary number "0101" looks like "1010" when read upside down and the binary number "110" looks like "011" (the Chef uses simple vertical lines to denote a 1 bit). The Chef didn't realize that the servers would read the numbers upside down so he doesn't rotate the paper when he places it on the counter. Thus, when the server picks up an appetizer they place it the location indexed by the binary number when it is read upside down.
You are given the message the chef intended to display and you are to display the message that will be displayed after the servers move all appetizers to their locations based on the binary numbers they read.
------ Input ------
The first line consists of a single integer T ≤ 25 indicating the number of test cases to follow. Each test case consists of a single line beginning with an integer 1 ≤ k ≤ 16 followed by a string of precisely 2^{k} characters. The integer and the string are separated by a single space. The string has no spaces and is composed only of lower case letters from a to z.
------ Output ------
For each test case you are to output the scrambled message on a single line.
----- Sample Input 1 ------
2
2 chef
4 enjoyourapplepie
----- Sample Output 1 ------
cehf
eayejpuinpopolre | import sys
input = sys.stdin.readline
def inp():
return int(input())
def inlt():
return list(map(int, input().split()))
def insr():
return input().strip()
def invr():
return map(int, input().split())
def outp(n):
sys.stdout.write(str(n) + "\n")
def outlt(lst):
sys.stdout.write(" ".join(map(str, lst)) + "\n")
def outplt(lst):
sys.stdout.write("\n".join(map(str, lst)))
def outpltlt(lst):
sys.stdout.write("\n".join(map(str, (" ".join(map(str, a)) for a in lst))))
ans = []
for _ in range(inp()):
k, s = input().split()
k = int(k)
N = 2**k
code = [int(bin(x)[2:].rjust(k, "0")[::-1], base=2) for x in range(N)]
an = [""] * N
for i, a in enumerate(s):
an[code[i]] = a
ans.append("".join(an))
outplt(ans) | IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL STRING FUNC_CALL VAR VAR VAR STRING FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR STRING NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST STRING VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR |
The Chef has prepared the appetizers in the shapes of letters to spell a special message for the guests. There are n appetizers numbered from 0 to n-1 such that if the appetizers are arrayed in this order, they will display the message. The Chef plans to display them in this order on a table that can be viewed by all guests as they enter. The appetizers will only be served once all guests are seated.
The appetizers are not necessarily finished in the same order as they are numbered. So, when an appetizer is finished the Chef will write the number on a piece of paper and place it beside the appetizer on a counter between the kitchen and the restaurant. A server will retrieve this appetizer and place it in the proper location according to the number written beside it.
The Chef has a penchant for binary numbers. The number of appetizers created is a power of 2, say n = 2^{k}. Furthermore, he has written the number of the appetizer in binary with exactly k bits. That is, binary numbers with fewer than k bits are padded on the left with zeros so they are written with exactly k bits.
Unfortunately, this has unforseen complications. A binary number still "looks" binary when it is written upside down. For example, the binary number "0101" looks like "1010" when read upside down and the binary number "110" looks like "011" (the Chef uses simple vertical lines to denote a 1 bit). The Chef didn't realize that the servers would read the numbers upside down so he doesn't rotate the paper when he places it on the counter. Thus, when the server picks up an appetizer they place it the location indexed by the binary number when it is read upside down.
You are given the message the chef intended to display and you are to display the message that will be displayed after the servers move all appetizers to their locations based on the binary numbers they read.
------ Input ------
The first line consists of a single integer T ≤ 25 indicating the number of test cases to follow. Each test case consists of a single line beginning with an integer 1 ≤ k ≤ 16 followed by a string of precisely 2^{k} characters. The integer and the string are separated by a single space. The string has no spaces and is composed only of lower case letters from a to z.
------ Output ------
For each test case you are to output the scrambled message on a single line.
----- Sample Input 1 ------
2
2 chef
4 enjoyourapplepie
----- Sample Output 1 ------
cehf
eayejpuinpopolre | def arrange(k, s):
t = [char for char in s]
def reverse_bits(n, k):
r = 0
while n:
if n & 1:
r |= 1 << k - 1
n >>= 1
k -= 1
return r
for pos in range(1, 2**k - 1):
t[reverse_bits(pos, k)] = s[pos]
return "".join(t)
t = int(input())
for _ in range(t):
k, s = input().split()
k = int(k)
print(arrange(k, s)) | FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
The Chef has prepared the appetizers in the shapes of letters to spell a special message for the guests. There are n appetizers numbered from 0 to n-1 such that if the appetizers are arrayed in this order, they will display the message. The Chef plans to display them in this order on a table that can be viewed by all guests as they enter. The appetizers will only be served once all guests are seated.
The appetizers are not necessarily finished in the same order as they are numbered. So, when an appetizer is finished the Chef will write the number on a piece of paper and place it beside the appetizer on a counter between the kitchen and the restaurant. A server will retrieve this appetizer and place it in the proper location according to the number written beside it.
The Chef has a penchant for binary numbers. The number of appetizers created is a power of 2, say n = 2^{k}. Furthermore, he has written the number of the appetizer in binary with exactly k bits. That is, binary numbers with fewer than k bits are padded on the left with zeros so they are written with exactly k bits.
Unfortunately, this has unforseen complications. A binary number still "looks" binary when it is written upside down. For example, the binary number "0101" looks like "1010" when read upside down and the binary number "110" looks like "011" (the Chef uses simple vertical lines to denote a 1 bit). The Chef didn't realize that the servers would read the numbers upside down so he doesn't rotate the paper when he places it on the counter. Thus, when the server picks up an appetizer they place it the location indexed by the binary number when it is read upside down.
You are given the message the chef intended to display and you are to display the message that will be displayed after the servers move all appetizers to their locations based on the binary numbers they read.
------ Input ------
The first line consists of a single integer T ≤ 25 indicating the number of test cases to follow. Each test case consists of a single line beginning with an integer 1 ≤ k ≤ 16 followed by a string of precisely 2^{k} characters. The integer and the string are separated by a single space. The string has no spaces and is composed only of lower case letters from a to z.
------ Output ------
For each test case you are to output the scrambled message on a single line.
----- Sample Input 1 ------
2
2 chef
4 enjoyourapplepie
----- Sample Output 1 ------
cehf
eayejpuinpopolre | for test_case in range(int(input())):
input1 = input().split(" ")
bits = input1[0]
string = input1[1]
new_str = []
for i in range(len(string)):
new_str.append("")
for i in range(len(string)):
og_letter = string[i]
bin_str = "{0:0" + bits + "b}"
binary = bin_str.format(i)
bin_reverse = binary[::-1]
reverse = int(binary[::-1], 2)
new_str[reverse] = string[i]
print("".join(new_str)) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
The Chef has prepared the appetizers in the shapes of letters to spell a special message for the guests. There are n appetizers numbered from 0 to n-1 such that if the appetizers are arrayed in this order, they will display the message. The Chef plans to display them in this order on a table that can be viewed by all guests as they enter. The appetizers will only be served once all guests are seated.
The appetizers are not necessarily finished in the same order as they are numbered. So, when an appetizer is finished the Chef will write the number on a piece of paper and place it beside the appetizer on a counter between the kitchen and the restaurant. A server will retrieve this appetizer and place it in the proper location according to the number written beside it.
The Chef has a penchant for binary numbers. The number of appetizers created is a power of 2, say n = 2^{k}. Furthermore, he has written the number of the appetizer in binary with exactly k bits. That is, binary numbers with fewer than k bits are padded on the left with zeros so they are written with exactly k bits.
Unfortunately, this has unforseen complications. A binary number still "looks" binary when it is written upside down. For example, the binary number "0101" looks like "1010" when read upside down and the binary number "110" looks like "011" (the Chef uses simple vertical lines to denote a 1 bit). The Chef didn't realize that the servers would read the numbers upside down so he doesn't rotate the paper when he places it on the counter. Thus, when the server picks up an appetizer they place it the location indexed by the binary number when it is read upside down.
You are given the message the chef intended to display and you are to display the message that will be displayed after the servers move all appetizers to their locations based on the binary numbers they read.
------ Input ------
The first line consists of a single integer T ≤ 25 indicating the number of test cases to follow. Each test case consists of a single line beginning with an integer 1 ≤ k ≤ 16 followed by a string of precisely 2^{k} characters. The integer and the string are separated by a single space. The string has no spaces and is composed only of lower case letters from a to z.
------ Output ------
For each test case you are to output the scrambled message on a single line.
----- Sample Input 1 ------
2
2 chef
4 enjoyourapplepie
----- Sample Output 1 ------
cehf
eayejpuinpopolre | a = int(input())
for i in range(a):
b = list(map(str, input().split()))
l = []
for j in range(len(b[1])):
c = bin(j)
c = "0" * (int(b[0]) - (len(c) - 2)) + c[2:]
l.append(c[::-1])
c = []
for j in range(len(l)):
c.append(b[1][int(l[j], 2)])
print("".join(c)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
The Chef has prepared the appetizers in the shapes of letters to spell a special message for the guests. There are n appetizers numbered from 0 to n-1 such that if the appetizers are arrayed in this order, they will display the message. The Chef plans to display them in this order on a table that can be viewed by all guests as they enter. The appetizers will only be served once all guests are seated.
The appetizers are not necessarily finished in the same order as they are numbered. So, when an appetizer is finished the Chef will write the number on a piece of paper and place it beside the appetizer on a counter between the kitchen and the restaurant. A server will retrieve this appetizer and place it in the proper location according to the number written beside it.
The Chef has a penchant for binary numbers. The number of appetizers created is a power of 2, say n = 2^{k}. Furthermore, he has written the number of the appetizer in binary with exactly k bits. That is, binary numbers with fewer than k bits are padded on the left with zeros so they are written with exactly k bits.
Unfortunately, this has unforseen complications. A binary number still "looks" binary when it is written upside down. For example, the binary number "0101" looks like "1010" when read upside down and the binary number "110" looks like "011" (the Chef uses simple vertical lines to denote a 1 bit). The Chef didn't realize that the servers would read the numbers upside down so he doesn't rotate the paper when he places it on the counter. Thus, when the server picks up an appetizer they place it the location indexed by the binary number when it is read upside down.
You are given the message the chef intended to display and you are to display the message that will be displayed after the servers move all appetizers to their locations based on the binary numbers they read.
------ Input ------
The first line consists of a single integer T ≤ 25 indicating the number of test cases to follow. Each test case consists of a single line beginning with an integer 1 ≤ k ≤ 16 followed by a string of precisely 2^{k} characters. The integer and the string are separated by a single space. The string has no spaces and is composed only of lower case letters from a to z.
------ Output ------
For each test case you are to output the scrambled message on a single line.
----- Sample Input 1 ------
2
2 chef
4 enjoyourapplepie
----- Sample Output 1 ------
cehf
eayejpuinpopolre | def rev(i, n):
binary = bin(i)
reverse = binary[-1:1:-1]
reverse = reverse + (n - len(reverse)) * "0"
return int(reverse, 2)
t = int(input())
while t:
n, l = input().split()
n = int(n)
list = [char for char in l]
for i in range(2**n):
j = rev(i, n)
if i > j:
list[i], list[j] = list[j], list[i]
list = "".join(list)
print(list)
t -= 1 | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR STRING RETURN FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR VAR NUMBER |
The Chef has prepared the appetizers in the shapes of letters to spell a special message for the guests. There are n appetizers numbered from 0 to n-1 such that if the appetizers are arrayed in this order, they will display the message. The Chef plans to display them in this order on a table that can be viewed by all guests as they enter. The appetizers will only be served once all guests are seated.
The appetizers are not necessarily finished in the same order as they are numbered. So, when an appetizer is finished the Chef will write the number on a piece of paper and place it beside the appetizer on a counter between the kitchen and the restaurant. A server will retrieve this appetizer and place it in the proper location according to the number written beside it.
The Chef has a penchant for binary numbers. The number of appetizers created is a power of 2, say n = 2^{k}. Furthermore, he has written the number of the appetizer in binary with exactly k bits. That is, binary numbers with fewer than k bits are padded on the left with zeros so they are written with exactly k bits.
Unfortunately, this has unforseen complications. A binary number still "looks" binary when it is written upside down. For example, the binary number "0101" looks like "1010" when read upside down and the binary number "110" looks like "011" (the Chef uses simple vertical lines to denote a 1 bit). The Chef didn't realize that the servers would read the numbers upside down so he doesn't rotate the paper when he places it on the counter. Thus, when the server picks up an appetizer they place it the location indexed by the binary number when it is read upside down.
You are given the message the chef intended to display and you are to display the message that will be displayed after the servers move all appetizers to their locations based on the binary numbers they read.
------ Input ------
The first line consists of a single integer T ≤ 25 indicating the number of test cases to follow. Each test case consists of a single line beginning with an integer 1 ≤ k ≤ 16 followed by a string of precisely 2^{k} characters. The integer and the string are separated by a single space. The string has no spaces and is composed only of lower case letters from a to z.
------ Output ------
For each test case you are to output the scrambled message on a single line.
----- Sample Input 1 ------
2
2 chef
4 enjoyourapplepie
----- Sample Output 1 ------
cehf
eayejpuinpopolre | t = int(input())
for i in range(0, t):
n, a = input().strip().split()
n = int(n)
for j in range(0, 2**n):
c = bin(j)[2:]
c = "0" * (n - len(c)) + c
c = c[::-1]
print(a[int(c, 2)], end="")
print() | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER STRING EXPR FUNC_CALL VAR |
The Chef has prepared the appetizers in the shapes of letters to spell a special message for the guests. There are n appetizers numbered from 0 to n-1 such that if the appetizers are arrayed in this order, they will display the message. The Chef plans to display them in this order on a table that can be viewed by all guests as they enter. The appetizers will only be served once all guests are seated.
The appetizers are not necessarily finished in the same order as they are numbered. So, when an appetizer is finished the Chef will write the number on a piece of paper and place it beside the appetizer on a counter between the kitchen and the restaurant. A server will retrieve this appetizer and place it in the proper location according to the number written beside it.
The Chef has a penchant for binary numbers. The number of appetizers created is a power of 2, say n = 2^{k}. Furthermore, he has written the number of the appetizer in binary with exactly k bits. That is, binary numbers with fewer than k bits are padded on the left with zeros so they are written with exactly k bits.
Unfortunately, this has unforseen complications. A binary number still "looks" binary when it is written upside down. For example, the binary number "0101" looks like "1010" when read upside down and the binary number "110" looks like "011" (the Chef uses simple vertical lines to denote a 1 bit). The Chef didn't realize that the servers would read the numbers upside down so he doesn't rotate the paper when he places it on the counter. Thus, when the server picks up an appetizer they place it the location indexed by the binary number when it is read upside down.
You are given the message the chef intended to display and you are to display the message that will be displayed after the servers move all appetizers to their locations based on the binary numbers they read.
------ Input ------
The first line consists of a single integer T ≤ 25 indicating the number of test cases to follow. Each test case consists of a single line beginning with an integer 1 ≤ k ≤ 16 followed by a string of precisely 2^{k} characters. The integer and the string are separated by a single space. The string has no spaces and is composed only of lower case letters from a to z.
------ Output ------
For each test case you are to output the scrambled message on a single line.
----- Sample Input 1 ------
2
2 chef
4 enjoyourapplepie
----- Sample Output 1 ------
cehf
eayejpuinpopolre | for i in range(int(input())):
k, s = map(str, input().split())
k = int(k)
a = ""
for i in range(2**k):
v = 0
x = k
while i > 0:
v = v + 2 ** (x - 1) * (i % 2)
i = i // 2
x = x - 1
a = a + s[int(v)]
print(a) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
The Chef has prepared the appetizers in the shapes of letters to spell a special message for the guests. There are n appetizers numbered from 0 to n-1 such that if the appetizers are arrayed in this order, they will display the message. The Chef plans to display them in this order on a table that can be viewed by all guests as they enter. The appetizers will only be served once all guests are seated.
The appetizers are not necessarily finished in the same order as they are numbered. So, when an appetizer is finished the Chef will write the number on a piece of paper and place it beside the appetizer on a counter between the kitchen and the restaurant. A server will retrieve this appetizer and place it in the proper location according to the number written beside it.
The Chef has a penchant for binary numbers. The number of appetizers created is a power of 2, say n = 2^{k}. Furthermore, he has written the number of the appetizer in binary with exactly k bits. That is, binary numbers with fewer than k bits are padded on the left with zeros so they are written with exactly k bits.
Unfortunately, this has unforseen complications. A binary number still "looks" binary when it is written upside down. For example, the binary number "0101" looks like "1010" when read upside down and the binary number "110" looks like "011" (the Chef uses simple vertical lines to denote a 1 bit). The Chef didn't realize that the servers would read the numbers upside down so he doesn't rotate the paper when he places it on the counter. Thus, when the server picks up an appetizer they place it the location indexed by the binary number when it is read upside down.
You are given the message the chef intended to display and you are to display the message that will be displayed after the servers move all appetizers to their locations based on the binary numbers they read.
------ Input ------
The first line consists of a single integer T ≤ 25 indicating the number of test cases to follow. Each test case consists of a single line beginning with an integer 1 ≤ k ≤ 16 followed by a string of precisely 2^{k} characters. The integer and the string are separated by a single space. The string has no spaces and is composed only of lower case letters from a to z.
------ Output ------
For each test case you are to output the scrambled message on a single line.
----- Sample Input 1 ------
2
2 chef
4 enjoyourapplepie
----- Sample Output 1 ------
cehf
eayejpuinpopolre | def get_binary(num, bits):
ans = []
for i in range(bits):
ans.append("0")
i = bits - 1
while i >= 0:
ans[i] = str(num % 2)
num //= 2
i -= 1
return "".join(ans)
for t in range(int(input())):
temp = list(input().split())
k = int(temp[0])
s = temp[1]
ans = []
for i in range(2**k):
ans.append("0")
for i in range(2**k):
bi = get_binary(i, k)
bi = bi[::-1]
x = eval("0b" + bi)
ans[x] = s[i]
print("".join(ans)) | FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN FUNC_CALL STRING VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP STRING VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
The Chef has prepared the appetizers in the shapes of letters to spell a special message for the guests. There are n appetizers numbered from 0 to n-1 such that if the appetizers are arrayed in this order, they will display the message. The Chef plans to display them in this order on a table that can be viewed by all guests as they enter. The appetizers will only be served once all guests are seated.
The appetizers are not necessarily finished in the same order as they are numbered. So, when an appetizer is finished the Chef will write the number on a piece of paper and place it beside the appetizer on a counter between the kitchen and the restaurant. A server will retrieve this appetizer and place it in the proper location according to the number written beside it.
The Chef has a penchant for binary numbers. The number of appetizers created is a power of 2, say n = 2^{k}. Furthermore, he has written the number of the appetizer in binary with exactly k bits. That is, binary numbers with fewer than k bits are padded on the left with zeros so they are written with exactly k bits.
Unfortunately, this has unforseen complications. A binary number still "looks" binary when it is written upside down. For example, the binary number "0101" looks like "1010" when read upside down and the binary number "110" looks like "011" (the Chef uses simple vertical lines to denote a 1 bit). The Chef didn't realize that the servers would read the numbers upside down so he doesn't rotate the paper when he places it on the counter. Thus, when the server picks up an appetizer they place it the location indexed by the binary number when it is read upside down.
You are given the message the chef intended to display and you are to display the message that will be displayed after the servers move all appetizers to their locations based on the binary numbers they read.
------ Input ------
The first line consists of a single integer T ≤ 25 indicating the number of test cases to follow. Each test case consists of a single line beginning with an integer 1 ≤ k ≤ 16 followed by a string of precisely 2^{k} characters. The integer and the string are separated by a single space. The string has no spaces and is composed only of lower case letters from a to z.
------ Output ------
For each test case you are to output the scrambled message on a single line.
----- Sample Input 1 ------
2
2 chef
4 enjoyourapplepie
----- Sample Output 1 ------
cehf
eayejpuinpopolre | t = int(input())
def binStrToInt(binary_str):
length = len(binary_str)
num = 0
for i in range(length):
num = num + int(binary_str[i])
num = num * 2
return num // 2
for index in range(t):
inp = input().split()
k = int(inp[0])
word = inp[1]
formatStr = "{0:0" + str(k) + "b}"
indices = [formatStr.format(x, "b") for x in range(len(word))]
for i in range(len(indices)):
indices[i] = indices[i][::-1]
for i in range(len(indices)):
indices[i] = binStrToInt(indices[i])
for i in indices:
print(word[int(i)], end="")
print() | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR STRING VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR |
The Chef has prepared the appetizers in the shapes of letters to spell a special message for the guests. There are n appetizers numbered from 0 to n-1 such that if the appetizers are arrayed in this order, they will display the message. The Chef plans to display them in this order on a table that can be viewed by all guests as they enter. The appetizers will only be served once all guests are seated.
The appetizers are not necessarily finished in the same order as they are numbered. So, when an appetizer is finished the Chef will write the number on a piece of paper and place it beside the appetizer on a counter between the kitchen and the restaurant. A server will retrieve this appetizer and place it in the proper location according to the number written beside it.
The Chef has a penchant for binary numbers. The number of appetizers created is a power of 2, say n = 2^{k}. Furthermore, he has written the number of the appetizer in binary with exactly k bits. That is, binary numbers with fewer than k bits are padded on the left with zeros so they are written with exactly k bits.
Unfortunately, this has unforseen complications. A binary number still "looks" binary when it is written upside down. For example, the binary number "0101" looks like "1010" when read upside down and the binary number "110" looks like "011" (the Chef uses simple vertical lines to denote a 1 bit). The Chef didn't realize that the servers would read the numbers upside down so he doesn't rotate the paper when he places it on the counter. Thus, when the server picks up an appetizer they place it the location indexed by the binary number when it is read upside down.
You are given the message the chef intended to display and you are to display the message that will be displayed after the servers move all appetizers to their locations based on the binary numbers they read.
------ Input ------
The first line consists of a single integer T ≤ 25 indicating the number of test cases to follow. Each test case consists of a single line beginning with an integer 1 ≤ k ≤ 16 followed by a string of precisely 2^{k} characters. The integer and the string are separated by a single space. The string has no spaces and is composed only of lower case letters from a to z.
------ Output ------
For each test case you are to output the scrambled message on a single line.
----- Sample Input 1 ------
2
2 chef
4 enjoyourapplepie
----- Sample Output 1 ------
cehf
eayejpuinpopolre | def padded_bin(i, width):
s = bin(i)
return s[:2] + s[2:].zfill(width)
t = int(input())
while t:
k, s = map(str, input().split())
k = int(k)
l = list()
new = list()
length = len(s)
for i in s:
l.append(i)
for i in range(0, length):
j = padded_bin(i, k)
j = j[:1:-1]
n = int(j, 2)
new.append(l[n])
u = "".join(new)
print(u)
t -= 1 | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR VAR NUMBER |
The Chef has prepared the appetizers in the shapes of letters to spell a special message for the guests. There are n appetizers numbered from 0 to n-1 such that if the appetizers are arrayed in this order, they will display the message. The Chef plans to display them in this order on a table that can be viewed by all guests as they enter. The appetizers will only be served once all guests are seated.
The appetizers are not necessarily finished in the same order as they are numbered. So, when an appetizer is finished the Chef will write the number on a piece of paper and place it beside the appetizer on a counter between the kitchen and the restaurant. A server will retrieve this appetizer and place it in the proper location according to the number written beside it.
The Chef has a penchant for binary numbers. The number of appetizers created is a power of 2, say n = 2^{k}. Furthermore, he has written the number of the appetizer in binary with exactly k bits. That is, binary numbers with fewer than k bits are padded on the left with zeros so they are written with exactly k bits.
Unfortunately, this has unforseen complications. A binary number still "looks" binary when it is written upside down. For example, the binary number "0101" looks like "1010" when read upside down and the binary number "110" looks like "011" (the Chef uses simple vertical lines to denote a 1 bit). The Chef didn't realize that the servers would read the numbers upside down so he doesn't rotate the paper when he places it on the counter. Thus, when the server picks up an appetizer they place it the location indexed by the binary number when it is read upside down.
You are given the message the chef intended to display and you are to display the message that will be displayed after the servers move all appetizers to their locations based on the binary numbers they read.
------ Input ------
The first line consists of a single integer T ≤ 25 indicating the number of test cases to follow. Each test case consists of a single line beginning with an integer 1 ≤ k ≤ 16 followed by a string of precisely 2^{k} characters. The integer and the string are separated by a single space. The string has no spaces and is composed only of lower case letters from a to z.
------ Output ------
For each test case you are to output the scrambled message on a single line.
----- Sample Input 1 ------
2
2 chef
4 enjoyourapplepie
----- Sample Output 1 ------
cehf
eayejpuinpopolre | t = int(input())
while t:
t -= 1
n, a = input().split()
n = int(n)
ans = ""
for i in range(2**n):
ans += a[int(bin(i)[2:].rjust(n, "0")[::-1], 2)]
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR STRING NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
The Chef has prepared the appetizers in the shapes of letters to spell a special message for the guests. There are n appetizers numbered from 0 to n-1 such that if the appetizers are arrayed in this order, they will display the message. The Chef plans to display them in this order on a table that can be viewed by all guests as they enter. The appetizers will only be served once all guests are seated.
The appetizers are not necessarily finished in the same order as they are numbered. So, when an appetizer is finished the Chef will write the number on a piece of paper and place it beside the appetizer on a counter between the kitchen and the restaurant. A server will retrieve this appetizer and place it in the proper location according to the number written beside it.
The Chef has a penchant for binary numbers. The number of appetizers created is a power of 2, say n = 2^{k}. Furthermore, he has written the number of the appetizer in binary with exactly k bits. That is, binary numbers with fewer than k bits are padded on the left with zeros so they are written with exactly k bits.
Unfortunately, this has unforseen complications. A binary number still "looks" binary when it is written upside down. For example, the binary number "0101" looks like "1010" when read upside down and the binary number "110" looks like "011" (the Chef uses simple vertical lines to denote a 1 bit). The Chef didn't realize that the servers would read the numbers upside down so he doesn't rotate the paper when he places it on the counter. Thus, when the server picks up an appetizer they place it the location indexed by the binary number when it is read upside down.
You are given the message the chef intended to display and you are to display the message that will be displayed after the servers move all appetizers to their locations based on the binary numbers they read.
------ Input ------
The first line consists of a single integer T ≤ 25 indicating the number of test cases to follow. Each test case consists of a single line beginning with an integer 1 ≤ k ≤ 16 followed by a string of precisely 2^{k} characters. The integer and the string are separated by a single space. The string has no spaces and is composed only of lower case letters from a to z.
------ Output ------
For each test case you are to output the scrambled message on a single line.
----- Sample Input 1 ------
2
2 chef
4 enjoyourapplepie
----- Sample Output 1 ------
cehf
eayejpuinpopolre | for _ in range(int(input())):
array = list(input().split())
k = int(array[0])
length = 2**k
final = [0] * length
for i in range(length):
p = bin(i)[2:]
for TT in range(k - len(p)):
p = "0" + p
p = p[-1::-1]
final[int(p, 2)] = array[1][i]
print("".join(final)) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
The Chef has prepared the appetizers in the shapes of letters to spell a special message for the guests. There are n appetizers numbered from 0 to n-1 such that if the appetizers are arrayed in this order, they will display the message. The Chef plans to display them in this order on a table that can be viewed by all guests as they enter. The appetizers will only be served once all guests are seated.
The appetizers are not necessarily finished in the same order as they are numbered. So, when an appetizer is finished the Chef will write the number on a piece of paper and place it beside the appetizer on a counter between the kitchen and the restaurant. A server will retrieve this appetizer and place it in the proper location according to the number written beside it.
The Chef has a penchant for binary numbers. The number of appetizers created is a power of 2, say n = 2^{k}. Furthermore, he has written the number of the appetizer in binary with exactly k bits. That is, binary numbers with fewer than k bits are padded on the left with zeros so they are written with exactly k bits.
Unfortunately, this has unforseen complications. A binary number still "looks" binary when it is written upside down. For example, the binary number "0101" looks like "1010" when read upside down and the binary number "110" looks like "011" (the Chef uses simple vertical lines to denote a 1 bit). The Chef didn't realize that the servers would read the numbers upside down so he doesn't rotate the paper when he places it on the counter. Thus, when the server picks up an appetizer they place it the location indexed by the binary number when it is read upside down.
You are given the message the chef intended to display and you are to display the message that will be displayed after the servers move all appetizers to their locations based on the binary numbers they read.
------ Input ------
The first line consists of a single integer T ≤ 25 indicating the number of test cases to follow. Each test case consists of a single line beginning with an integer 1 ≤ k ≤ 16 followed by a string of precisely 2^{k} characters. The integer and the string are separated by a single space. The string has no spaces and is composed only of lower case letters from a to z.
------ Output ------
For each test case you are to output the scrambled message on a single line.
----- Sample Input 1 ------
2
2 chef
4 enjoyourapplepie
----- Sample Output 1 ------
cehf
eayejpuinpopolre | for t in range(int(input())):
k, msg = input().strip().split()
k = int(k)
msg = list(msg)
new_msg = ["0"] * 2**k
for i in range(0, 2**k):
binary = bin(i)
rev = binary[-1:1:-1]
rev = rev + (k - len(rev)) * "0"
new_msg[int(rev, 2)] = msg[i]
print("".join(str(i) for i in new_msg)) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST STRING BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR |
The Chef has prepared the appetizers in the shapes of letters to spell a special message for the guests. There are n appetizers numbered from 0 to n-1 such that if the appetizers are arrayed in this order, they will display the message. The Chef plans to display them in this order on a table that can be viewed by all guests as they enter. The appetizers will only be served once all guests are seated.
The appetizers are not necessarily finished in the same order as they are numbered. So, when an appetizer is finished the Chef will write the number on a piece of paper and place it beside the appetizer on a counter between the kitchen and the restaurant. A server will retrieve this appetizer and place it in the proper location according to the number written beside it.
The Chef has a penchant for binary numbers. The number of appetizers created is a power of 2, say n = 2^{k}. Furthermore, he has written the number of the appetizer in binary with exactly k bits. That is, binary numbers with fewer than k bits are padded on the left with zeros so they are written with exactly k bits.
Unfortunately, this has unforseen complications. A binary number still "looks" binary when it is written upside down. For example, the binary number "0101" looks like "1010" when read upside down and the binary number "110" looks like "011" (the Chef uses simple vertical lines to denote a 1 bit). The Chef didn't realize that the servers would read the numbers upside down so he doesn't rotate the paper when he places it on the counter. Thus, when the server picks up an appetizer they place it the location indexed by the binary number when it is read upside down.
You are given the message the chef intended to display and you are to display the message that will be displayed after the servers move all appetizers to their locations based on the binary numbers they read.
------ Input ------
The first line consists of a single integer T ≤ 25 indicating the number of test cases to follow. Each test case consists of a single line beginning with an integer 1 ≤ k ≤ 16 followed by a string of precisely 2^{k} characters. The integer and the string are separated by a single space. The string has no spaces and is composed only of lower case letters from a to z.
------ Output ------
For each test case you are to output the scrambled message on a single line.
----- Sample Input 1 ------
2
2 chef
4 enjoyourapplepie
----- Sample Output 1 ------
cehf
eayejpuinpopolre | def binary(a, n):
ans = []
while a != 0:
ans.append(a & 1)
a >>= 1
temp = []
for x in range(n - len(ans)):
temp.append(0)
return int("".join(map(str, ans + temp)), 2)
for w in range(int(input())):
line = input().split()
k = int(line[0])
line = line[1]
ans = ""
for x in range(len(line)):
ans += line[binary(x, k)]
print(ans) | FUNC_DEF ASSIGN VAR LIST WHILE VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
The Chef has prepared the appetizers in the shapes of letters to spell a special message for the guests. There are n appetizers numbered from 0 to n-1 such that if the appetizers are arrayed in this order, they will display the message. The Chef plans to display them in this order on a table that can be viewed by all guests as they enter. The appetizers will only be served once all guests are seated.
The appetizers are not necessarily finished in the same order as they are numbered. So, when an appetizer is finished the Chef will write the number on a piece of paper and place it beside the appetizer on a counter between the kitchen and the restaurant. A server will retrieve this appetizer and place it in the proper location according to the number written beside it.
The Chef has a penchant for binary numbers. The number of appetizers created is a power of 2, say n = 2^{k}. Furthermore, he has written the number of the appetizer in binary with exactly k bits. That is, binary numbers with fewer than k bits are padded on the left with zeros so they are written with exactly k bits.
Unfortunately, this has unforseen complications. A binary number still "looks" binary when it is written upside down. For example, the binary number "0101" looks like "1010" when read upside down and the binary number "110" looks like "011" (the Chef uses simple vertical lines to denote a 1 bit). The Chef didn't realize that the servers would read the numbers upside down so he doesn't rotate the paper when he places it on the counter. Thus, when the server picks up an appetizer they place it the location indexed by the binary number when it is read upside down.
You are given the message the chef intended to display and you are to display the message that will be displayed after the servers move all appetizers to their locations based on the binary numbers they read.
------ Input ------
The first line consists of a single integer T ≤ 25 indicating the number of test cases to follow. Each test case consists of a single line beginning with an integer 1 ≤ k ≤ 16 followed by a string of precisely 2^{k} characters. The integer and the string are separated by a single space. The string has no spaces and is composed only of lower case letters from a to z.
------ Output ------
For each test case you are to output the scrambled message on a single line.
----- Sample Input 1 ------
2
2 chef
4 enjoyourapplepie
----- Sample Output 1 ------
cehf
eayejpuinpopolre | def convert_to_num(l):
l = list(map(int, l))
k = len(l)
num = 0
i = 0
while i < k:
num += l[i] * 2**i
i += 1
return num
def to_binary(l, k):
t = list(l)
if len(t) == k:
return t
t_1 = k - len(t)
while t_1 > 0:
t.insert(0, "0")
t_1 -= 1
return t
for _ in range(int(input())):
n, lis = input().split()
n = int(n)
lis = list(lis)
conv = {}
for i in range(len(lis)):
t_1 = bin(i)[2:]
t_2 = convert_to_num(to_binary(t_1, n))
conv[i] = t_2
ans = ""
for i in conv:
ans += lis[conv[i]]
print(ans) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR VAR BIN_OP NUMBER VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR NUMBER STRING VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR STRING FOR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
The Chef has prepared the appetizers in the shapes of letters to spell a special message for the guests. There are n appetizers numbered from 0 to n-1 such that if the appetizers are arrayed in this order, they will display the message. The Chef plans to display them in this order on a table that can be viewed by all guests as they enter. The appetizers will only be served once all guests are seated.
The appetizers are not necessarily finished in the same order as they are numbered. So, when an appetizer is finished the Chef will write the number on a piece of paper and place it beside the appetizer on a counter between the kitchen and the restaurant. A server will retrieve this appetizer and place it in the proper location according to the number written beside it.
The Chef has a penchant for binary numbers. The number of appetizers created is a power of 2, say n = 2^{k}. Furthermore, he has written the number of the appetizer in binary with exactly k bits. That is, binary numbers with fewer than k bits are padded on the left with zeros so they are written with exactly k bits.
Unfortunately, this has unforseen complications. A binary number still "looks" binary when it is written upside down. For example, the binary number "0101" looks like "1010" when read upside down and the binary number "110" looks like "011" (the Chef uses simple vertical lines to denote a 1 bit). The Chef didn't realize that the servers would read the numbers upside down so he doesn't rotate the paper when he places it on the counter. Thus, when the server picks up an appetizer they place it the location indexed by the binary number when it is read upside down.
You are given the message the chef intended to display and you are to display the message that will be displayed after the servers move all appetizers to their locations based on the binary numbers they read.
------ Input ------
The first line consists of a single integer T ≤ 25 indicating the number of test cases to follow. Each test case consists of a single line beginning with an integer 1 ≤ k ≤ 16 followed by a string of precisely 2^{k} characters. The integer and the string are separated by a single space. The string has no spaces and is composed only of lower case letters from a to z.
------ Output ------
For each test case you are to output the scrambled message on a single line.
----- Sample Input 1 ------
2
2 chef
4 enjoyourapplepie
----- Sample Output 1 ------
cehf
eayejpuinpopolre | for t in range(int(input())):
n, word = input().split()
n = int(n)
length = 2**n
new_word = ""
for i in range(length):
binary = "{0:b}".format(i, n)
if len(binary) < n:
binary = "0" * (n - len(binary)) + binary
pos = int(binary[::-1], 2)
new_word += word[pos]
print(new_word) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR |
The Chef has prepared the appetizers in the shapes of letters to spell a special message for the guests. There are n appetizers numbered from 0 to n-1 such that if the appetizers are arrayed in this order, they will display the message. The Chef plans to display them in this order on a table that can be viewed by all guests as they enter. The appetizers will only be served once all guests are seated.
The appetizers are not necessarily finished in the same order as they are numbered. So, when an appetizer is finished the Chef will write the number on a piece of paper and place it beside the appetizer on a counter between the kitchen and the restaurant. A server will retrieve this appetizer and place it in the proper location according to the number written beside it.
The Chef has a penchant for binary numbers. The number of appetizers created is a power of 2, say n = 2^{k}. Furthermore, he has written the number of the appetizer in binary with exactly k bits. That is, binary numbers with fewer than k bits are padded on the left with zeros so they are written with exactly k bits.
Unfortunately, this has unforseen complications. A binary number still "looks" binary when it is written upside down. For example, the binary number "0101" looks like "1010" when read upside down and the binary number "110" looks like "011" (the Chef uses simple vertical lines to denote a 1 bit). The Chef didn't realize that the servers would read the numbers upside down so he doesn't rotate the paper when he places it on the counter. Thus, when the server picks up an appetizer they place it the location indexed by the binary number when it is read upside down.
You are given the message the chef intended to display and you are to display the message that will be displayed after the servers move all appetizers to their locations based on the binary numbers they read.
------ Input ------
The first line consists of a single integer T ≤ 25 indicating the number of test cases to follow. Each test case consists of a single line beginning with an integer 1 ≤ k ≤ 16 followed by a string of precisely 2^{k} characters. The integer and the string are separated by a single space. The string has no spaces and is composed only of lower case letters from a to z.
------ Output ------
For each test case you are to output the scrambled message on a single line.
----- Sample Input 1 ------
2
2 chef
4 enjoyourapplepie
----- Sample Output 1 ------
cehf
eayejpuinpopolre | t = int(input())
for z in range(t):
dictionary = {}
k, string = map(str, input().split())
k = int(k)
for i in range(2**k):
dictionary[str(bin(i)[2:].zfill(k))] = string[i]
for i in range(2**k):
alpha = bin(i)[2:].zfill(k)
alpha = alpha[::-1]
print(dictionary[str(alpha)], end="")
print() | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR |
The Chef has prepared the appetizers in the shapes of letters to spell a special message for the guests. There are n appetizers numbered from 0 to n-1 such that if the appetizers are arrayed in this order, they will display the message. The Chef plans to display them in this order on a table that can be viewed by all guests as they enter. The appetizers will only be served once all guests are seated.
The appetizers are not necessarily finished in the same order as they are numbered. So, when an appetizer is finished the Chef will write the number on a piece of paper and place it beside the appetizer on a counter between the kitchen and the restaurant. A server will retrieve this appetizer and place it in the proper location according to the number written beside it.
The Chef has a penchant for binary numbers. The number of appetizers created is a power of 2, say n = 2^{k}. Furthermore, he has written the number of the appetizer in binary with exactly k bits. That is, binary numbers with fewer than k bits are padded on the left with zeros so they are written with exactly k bits.
Unfortunately, this has unforseen complications. A binary number still "looks" binary when it is written upside down. For example, the binary number "0101" looks like "1010" when read upside down and the binary number "110" looks like "011" (the Chef uses simple vertical lines to denote a 1 bit). The Chef didn't realize that the servers would read the numbers upside down so he doesn't rotate the paper when he places it on the counter. Thus, when the server picks up an appetizer they place it the location indexed by the binary number when it is read upside down.
You are given the message the chef intended to display and you are to display the message that will be displayed after the servers move all appetizers to their locations based on the binary numbers they read.
------ Input ------
The first line consists of a single integer T ≤ 25 indicating the number of test cases to follow. Each test case consists of a single line beginning with an integer 1 ≤ k ≤ 16 followed by a string of precisely 2^{k} characters. The integer and the string are separated by a single space. The string has no spaces and is composed only of lower case letters from a to z.
------ Output ------
For each test case you are to output the scrambled message on a single line.
----- Sample Input 1 ------
2
2 chef
4 enjoyourapplepie
----- Sample Output 1 ------
cehf
eayejpuinpopolre | for tests in range(int(input())):
K, old = input().split()
new = [i for i in old]
K = int(K)
for letter in range(len(old)):
binary = bin(letter)[2:]
binary = "0" * (K - len(binary)) + binary
binary = "".join(binary[::-1])
new[int(binary, 2)] = old[letter]
print("".join(new)) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
The Chef has prepared the appetizers in the shapes of letters to spell a special message for the guests. There are n appetizers numbered from 0 to n-1 such that if the appetizers are arrayed in this order, they will display the message. The Chef plans to display them in this order on a table that can be viewed by all guests as they enter. The appetizers will only be served once all guests are seated.
The appetizers are not necessarily finished in the same order as they are numbered. So, when an appetizer is finished the Chef will write the number on a piece of paper and place it beside the appetizer on a counter between the kitchen and the restaurant. A server will retrieve this appetizer and place it in the proper location according to the number written beside it.
The Chef has a penchant for binary numbers. The number of appetizers created is a power of 2, say n = 2^{k}. Furthermore, he has written the number of the appetizer in binary with exactly k bits. That is, binary numbers with fewer than k bits are padded on the left with zeros so they are written with exactly k bits.
Unfortunately, this has unforseen complications. A binary number still "looks" binary when it is written upside down. For example, the binary number "0101" looks like "1010" when read upside down and the binary number "110" looks like "011" (the Chef uses simple vertical lines to denote a 1 bit). The Chef didn't realize that the servers would read the numbers upside down so he doesn't rotate the paper when he places it on the counter. Thus, when the server picks up an appetizer they place it the location indexed by the binary number when it is read upside down.
You are given the message the chef intended to display and you are to display the message that will be displayed after the servers move all appetizers to their locations based on the binary numbers they read.
------ Input ------
The first line consists of a single integer T ≤ 25 indicating the number of test cases to follow. Each test case consists of a single line beginning with an integer 1 ≤ k ≤ 16 followed by a string of precisely 2^{k} characters. The integer and the string are separated by a single space. The string has no spaces and is composed only of lower case letters from a to z.
------ Output ------
For each test case you are to output the scrambled message on a single line.
----- Sample Input 1 ------
2
2 chef
4 enjoyourapplepie
----- Sample Output 1 ------
cehf
eayejpuinpopolre | for t in range(int(input())):
k, text = map(str, input().split())
k = int(k)
dic = {(0): 0}
alp = ["."] * 2**k
for i in range(1, 2**k):
if i not in dic:
ibin = list(bin(i))
ibin = ibin[2:]
if len(ibin) != k:
ibin = ["0"] * (k - len(ibin)) + ibin
ibin.reverse()
ibin = ["0b"] + ibin
ibin = int("".join(xy for xy in ibin), 2)
dic[i] = ibin
dic[ibin] = i
for x in range(2**k):
alp[dic[x]] = text[x]
print("".join(xy for xy in alp)) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR BIN_OP LIST STRING BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST STRING BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR VAR |
The Chef has prepared the appetizers in the shapes of letters to spell a special message for the guests. There are n appetizers numbered from 0 to n-1 such that if the appetizers are arrayed in this order, they will display the message. The Chef plans to display them in this order on a table that can be viewed by all guests as they enter. The appetizers will only be served once all guests are seated.
The appetizers are not necessarily finished in the same order as they are numbered. So, when an appetizer is finished the Chef will write the number on a piece of paper and place it beside the appetizer on a counter between the kitchen and the restaurant. A server will retrieve this appetizer and place it in the proper location according to the number written beside it.
The Chef has a penchant for binary numbers. The number of appetizers created is a power of 2, say n = 2^{k}. Furthermore, he has written the number of the appetizer in binary with exactly k bits. That is, binary numbers with fewer than k bits are padded on the left with zeros so they are written with exactly k bits.
Unfortunately, this has unforseen complications. A binary number still "looks" binary when it is written upside down. For example, the binary number "0101" looks like "1010" when read upside down and the binary number "110" looks like "011" (the Chef uses simple vertical lines to denote a 1 bit). The Chef didn't realize that the servers would read the numbers upside down so he doesn't rotate the paper when he places it on the counter. Thus, when the server picks up an appetizer they place it the location indexed by the binary number when it is read upside down.
You are given the message the chef intended to display and you are to display the message that will be displayed after the servers move all appetizers to their locations based on the binary numbers they read.
------ Input ------
The first line consists of a single integer T ≤ 25 indicating the number of test cases to follow. Each test case consists of a single line beginning with an integer 1 ≤ k ≤ 16 followed by a string of precisely 2^{k} characters. The integer and the string are separated by a single space. The string has no spaces and is composed only of lower case letters from a to z.
------ Output ------
For each test case you are to output the scrambled message on a single line.
----- Sample Input 1 ------
2
2 chef
4 enjoyourapplepie
----- Sample Output 1 ------
cehf
eayejpuinpopolre | n = int(input())
for i in range(n):
st = input().split(" ")
k = int(st[0])
msg = list(st[1])
arr = [x for x in range(2**k)]
for i in range(len(msg)):
binNum = bin(i).replace("0b", "").zfill(k)
revbinIndex = int(binNum[::-1], 2)
arr[revbinIndex] = msg[i]
for i in arr:
print(i, end="")
print() | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR VAR STRING STRING VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR |
The Chef has prepared the appetizers in the shapes of letters to spell a special message for the guests. There are n appetizers numbered from 0 to n-1 such that if the appetizers are arrayed in this order, they will display the message. The Chef plans to display them in this order on a table that can be viewed by all guests as they enter. The appetizers will only be served once all guests are seated.
The appetizers are not necessarily finished in the same order as they are numbered. So, when an appetizer is finished the Chef will write the number on a piece of paper and place it beside the appetizer on a counter between the kitchen and the restaurant. A server will retrieve this appetizer and place it in the proper location according to the number written beside it.
The Chef has a penchant for binary numbers. The number of appetizers created is a power of 2, say n = 2^{k}. Furthermore, he has written the number of the appetizer in binary with exactly k bits. That is, binary numbers with fewer than k bits are padded on the left with zeros so they are written with exactly k bits.
Unfortunately, this has unforseen complications. A binary number still "looks" binary when it is written upside down. For example, the binary number "0101" looks like "1010" when read upside down and the binary number "110" looks like "011" (the Chef uses simple vertical lines to denote a 1 bit). The Chef didn't realize that the servers would read the numbers upside down so he doesn't rotate the paper when he places it on the counter. Thus, when the server picks up an appetizer they place it the location indexed by the binary number when it is read upside down.
You are given the message the chef intended to display and you are to display the message that will be displayed after the servers move all appetizers to their locations based on the binary numbers they read.
------ Input ------
The first line consists of a single integer T ≤ 25 indicating the number of test cases to follow. Each test case consists of a single line beginning with an integer 1 ≤ k ≤ 16 followed by a string of precisely 2^{k} characters. The integer and the string are separated by a single space. The string has no spaces and is composed only of lower case letters from a to z.
------ Output ------
For each test case you are to output the scrambled message on a single line.
----- Sample Input 1 ------
2
2 chef
4 enjoyourapplepie
----- Sample Output 1 ------
cehf
eayejpuinpopolre | for i in range(int(input())):
n, s = input().split()
l = list(s)
for i in range(len(s)):
b = bin(i)[2:]
b = int(("0" * (int(n) - len(b)) + b)[::-1], 2)
l[b] = s[i]
print("".join(l)) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP STRING BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
The Chef has prepared the appetizers in the shapes of letters to spell a special message for the guests. There are n appetizers numbered from 0 to n-1 such that if the appetizers are arrayed in this order, they will display the message. The Chef plans to display them in this order on a table that can be viewed by all guests as they enter. The appetizers will only be served once all guests are seated.
The appetizers are not necessarily finished in the same order as they are numbered. So, when an appetizer is finished the Chef will write the number on a piece of paper and place it beside the appetizer on a counter between the kitchen and the restaurant. A server will retrieve this appetizer and place it in the proper location according to the number written beside it.
The Chef has a penchant for binary numbers. The number of appetizers created is a power of 2, say n = 2^{k}. Furthermore, he has written the number of the appetizer in binary with exactly k bits. That is, binary numbers with fewer than k bits are padded on the left with zeros so they are written with exactly k bits.
Unfortunately, this has unforseen complications. A binary number still "looks" binary when it is written upside down. For example, the binary number "0101" looks like "1010" when read upside down and the binary number "110" looks like "011" (the Chef uses simple vertical lines to denote a 1 bit). The Chef didn't realize that the servers would read the numbers upside down so he doesn't rotate the paper when he places it on the counter. Thus, when the server picks up an appetizer they place it the location indexed by the binary number when it is read upside down.
You are given the message the chef intended to display and you are to display the message that will be displayed after the servers move all appetizers to their locations based on the binary numbers they read.
------ Input ------
The first line consists of a single integer T ≤ 25 indicating the number of test cases to follow. Each test case consists of a single line beginning with an integer 1 ≤ k ≤ 16 followed by a string of precisely 2^{k} characters. The integer and the string are separated by a single space. The string has no spaces and is composed only of lower case letters from a to z.
------ Output ------
For each test case you are to output the scrambled message on a single line.
----- Sample Input 1 ------
2
2 chef
4 enjoyourapplepie
----- Sample Output 1 ------
cehf
eayejpuinpopolre | t = int(input())
while t:
k, s = map(str, input().split())
k = int(k)
for i in range(2**k):
j = bin(i)[2:]
r = "0" * (k - len(j)) + j
print(s[int(r[::-1], 2)], end="")
print("")
t -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER STRING EXPR FUNC_CALL VAR STRING VAR NUMBER |
The Chef has prepared the appetizers in the shapes of letters to spell a special message for the guests. There are n appetizers numbered from 0 to n-1 such that if the appetizers are arrayed in this order, they will display the message. The Chef plans to display them in this order on a table that can be viewed by all guests as they enter. The appetizers will only be served once all guests are seated.
The appetizers are not necessarily finished in the same order as they are numbered. So, when an appetizer is finished the Chef will write the number on a piece of paper and place it beside the appetizer on a counter between the kitchen and the restaurant. A server will retrieve this appetizer and place it in the proper location according to the number written beside it.
The Chef has a penchant for binary numbers. The number of appetizers created is a power of 2, say n = 2^{k}. Furthermore, he has written the number of the appetizer in binary with exactly k bits. That is, binary numbers with fewer than k bits are padded on the left with zeros so they are written with exactly k bits.
Unfortunately, this has unforseen complications. A binary number still "looks" binary when it is written upside down. For example, the binary number "0101" looks like "1010" when read upside down and the binary number "110" looks like "011" (the Chef uses simple vertical lines to denote a 1 bit). The Chef didn't realize that the servers would read the numbers upside down so he doesn't rotate the paper when he places it on the counter. Thus, when the server picks up an appetizer they place it the location indexed by the binary number when it is read upside down.
You are given the message the chef intended to display and you are to display the message that will be displayed after the servers move all appetizers to their locations based on the binary numbers they read.
------ Input ------
The first line consists of a single integer T ≤ 25 indicating the number of test cases to follow. Each test case consists of a single line beginning with an integer 1 ≤ k ≤ 16 followed by a string of precisely 2^{k} characters. The integer and the string are separated by a single space. The string has no spaces and is composed only of lower case letters from a to z.
------ Output ------
For each test case you are to output the scrambled message on a single line.
----- Sample Input 1 ------
2
2 chef
4 enjoyourapplepie
----- Sample Output 1 ------
cehf
eayejpuinpopolre | n = int(input())
while n > 0:
a, b = input().split()
a = int(a)
s = [" " for i in range(len(b))]
for i in range(len(b)):
bin1 = str(bin(i))[2:]
bin1 = "0" * (a - len(bin1)) + bin1
bin1 = int(bin1[::-1], 2)
s[bin1] = b[i]
print("".join(s))
n -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR NUMBER |
The Chef has prepared the appetizers in the shapes of letters to spell a special message for the guests. There are n appetizers numbered from 0 to n-1 such that if the appetizers are arrayed in this order, they will display the message. The Chef plans to display them in this order on a table that can be viewed by all guests as they enter. The appetizers will only be served once all guests are seated.
The appetizers are not necessarily finished in the same order as they are numbered. So, when an appetizer is finished the Chef will write the number on a piece of paper and place it beside the appetizer on a counter between the kitchen and the restaurant. A server will retrieve this appetizer and place it in the proper location according to the number written beside it.
The Chef has a penchant for binary numbers. The number of appetizers created is a power of 2, say n = 2^{k}. Furthermore, he has written the number of the appetizer in binary with exactly k bits. That is, binary numbers with fewer than k bits are padded on the left with zeros so they are written with exactly k bits.
Unfortunately, this has unforseen complications. A binary number still "looks" binary when it is written upside down. For example, the binary number "0101" looks like "1010" when read upside down and the binary number "110" looks like "011" (the Chef uses simple vertical lines to denote a 1 bit). The Chef didn't realize that the servers would read the numbers upside down so he doesn't rotate the paper when he places it on the counter. Thus, when the server picks up an appetizer they place it the location indexed by the binary number when it is read upside down.
You are given the message the chef intended to display and you are to display the message that will be displayed after the servers move all appetizers to their locations based on the binary numbers they read.
------ Input ------
The first line consists of a single integer T ≤ 25 indicating the number of test cases to follow. Each test case consists of a single line beginning with an integer 1 ≤ k ≤ 16 followed by a string of precisely 2^{k} characters. The integer and the string are separated by a single space. The string has no spaces and is composed only of lower case letters from a to z.
------ Output ------
For each test case you are to output the scrambled message on a single line.
----- Sample Input 1 ------
2
2 chef
4 enjoyourapplepie
----- Sample Output 1 ------
cehf
eayejpuinpopolre | t = int(input())
for i in range(t):
inp = input()
arr = [(int(i) if i.isdigit() else i) for i in inp.split()]
li = [0] * 2 ** arr[0]
for j, k in enumerate(arr[1]):
ind = bin(j)[2:]
av = arr[0] - len(ind)
f = "0" * av + ind
mod_place = int(f[::-1], 2)
li[mod_place] = k
print("".join(li)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
The Chef has prepared the appetizers in the shapes of letters to spell a special message for the guests. There are n appetizers numbered from 0 to n-1 such that if the appetizers are arrayed in this order, they will display the message. The Chef plans to display them in this order on a table that can be viewed by all guests as they enter. The appetizers will only be served once all guests are seated.
The appetizers are not necessarily finished in the same order as they are numbered. So, when an appetizer is finished the Chef will write the number on a piece of paper and place it beside the appetizer on a counter between the kitchen and the restaurant. A server will retrieve this appetizer and place it in the proper location according to the number written beside it.
The Chef has a penchant for binary numbers. The number of appetizers created is a power of 2, say n = 2^{k}. Furthermore, he has written the number of the appetizer in binary with exactly k bits. That is, binary numbers with fewer than k bits are padded on the left with zeros so they are written with exactly k bits.
Unfortunately, this has unforseen complications. A binary number still "looks" binary when it is written upside down. For example, the binary number "0101" looks like "1010" when read upside down and the binary number "110" looks like "011" (the Chef uses simple vertical lines to denote a 1 bit). The Chef didn't realize that the servers would read the numbers upside down so he doesn't rotate the paper when he places it on the counter. Thus, when the server picks up an appetizer they place it the location indexed by the binary number when it is read upside down.
You are given the message the chef intended to display and you are to display the message that will be displayed after the servers move all appetizers to their locations based on the binary numbers they read.
------ Input ------
The first line consists of a single integer T ≤ 25 indicating the number of test cases to follow. Each test case consists of a single line beginning with an integer 1 ≤ k ≤ 16 followed by a string of precisely 2^{k} characters. The integer and the string are separated by a single space. The string has no spaces and is composed only of lower case letters from a to z.
------ Output ------
For each test case you are to output the scrambled message on a single line.
----- Sample Input 1 ------
2
2 chef
4 enjoyourapplepie
----- Sample Output 1 ------
cehf
eayejpuinpopolre | for _ in range(int(input())):
lst = list(input().split())
n, string = int(lst[0]), lst[1]
dictionary = dict()
for i in range(2**n):
temp = bin(i)[2:]
temp = "0" * (n - len(temp)) + temp
dictionary[temp] = string[i]
output = ""
for i in dictionary.keys():
rev = i[::-1]
output += dictionary[rev]
print(output) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR |
The Chef has prepared the appetizers in the shapes of letters to spell a special message for the guests. There are n appetizers numbered from 0 to n-1 such that if the appetizers are arrayed in this order, they will display the message. The Chef plans to display them in this order on a table that can be viewed by all guests as they enter. The appetizers will only be served once all guests are seated.
The appetizers are not necessarily finished in the same order as they are numbered. So, when an appetizer is finished the Chef will write the number on a piece of paper and place it beside the appetizer on a counter between the kitchen and the restaurant. A server will retrieve this appetizer and place it in the proper location according to the number written beside it.
The Chef has a penchant for binary numbers. The number of appetizers created is a power of 2, say n = 2^{k}. Furthermore, he has written the number of the appetizer in binary with exactly k bits. That is, binary numbers with fewer than k bits are padded on the left with zeros so they are written with exactly k bits.
Unfortunately, this has unforseen complications. A binary number still "looks" binary when it is written upside down. For example, the binary number "0101" looks like "1010" when read upside down and the binary number "110" looks like "011" (the Chef uses simple vertical lines to denote a 1 bit). The Chef didn't realize that the servers would read the numbers upside down so he doesn't rotate the paper when he places it on the counter. Thus, when the server picks up an appetizer they place it the location indexed by the binary number when it is read upside down.
You are given the message the chef intended to display and you are to display the message that will be displayed after the servers move all appetizers to their locations based on the binary numbers they read.
------ Input ------
The first line consists of a single integer T ≤ 25 indicating the number of test cases to follow. Each test case consists of a single line beginning with an integer 1 ≤ k ≤ 16 followed by a string of precisely 2^{k} characters. The integer and the string are separated by a single space. The string has no spaces and is composed only of lower case letters from a to z.
------ Output ------
For each test case you are to output the scrambled message on a single line.
----- Sample Input 1 ------
2
2 chef
4 enjoyourapplepie
----- Sample Output 1 ------
cehf
eayejpuinpopolre | allzero = "0000000000000000"
for _ in range(int(input())):
k, s = map(str, input().split())
k = int(k)
a = list(s)
b = ["a"] * pow(2, k)
for i in range(pow(2, k)):
t = bin(i)[2:].zfill(k)
rt = t[::-1]
b[int(rt, 2)] = a[i]
ans = "".join(e for e in b)
print(ans) | ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST STRING FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL STRING VAR VAR VAR EXPR FUNC_CALL VAR VAR |
The Chef has prepared the appetizers in the shapes of letters to spell a special message for the guests. There are n appetizers numbered from 0 to n-1 such that if the appetizers are arrayed in this order, they will display the message. The Chef plans to display them in this order on a table that can be viewed by all guests as they enter. The appetizers will only be served once all guests are seated.
The appetizers are not necessarily finished in the same order as they are numbered. So, when an appetizer is finished the Chef will write the number on a piece of paper and place it beside the appetizer on a counter between the kitchen and the restaurant. A server will retrieve this appetizer and place it in the proper location according to the number written beside it.
The Chef has a penchant for binary numbers. The number of appetizers created is a power of 2, say n = 2^{k}. Furthermore, he has written the number of the appetizer in binary with exactly k bits. That is, binary numbers with fewer than k bits are padded on the left with zeros so they are written with exactly k bits.
Unfortunately, this has unforseen complications. A binary number still "looks" binary when it is written upside down. For example, the binary number "0101" looks like "1010" when read upside down and the binary number "110" looks like "011" (the Chef uses simple vertical lines to denote a 1 bit). The Chef didn't realize that the servers would read the numbers upside down so he doesn't rotate the paper when he places it on the counter. Thus, when the server picks up an appetizer they place it the location indexed by the binary number when it is read upside down.
You are given the message the chef intended to display and you are to display the message that will be displayed after the servers move all appetizers to their locations based on the binary numbers they read.
------ Input ------
The first line consists of a single integer T ≤ 25 indicating the number of test cases to follow. Each test case consists of a single line beginning with an integer 1 ≤ k ≤ 16 followed by a string of precisely 2^{k} characters. The integer and the string are separated by a single space. The string has no spaces and is composed only of lower case letters from a to z.
------ Output ------
For each test case you are to output the scrambled message on a single line.
----- Sample Input 1 ------
2
2 chef
4 enjoyourapplepie
----- Sample Output 1 ------
cehf
eayejpuinpopolre | from sys import stdin, stdout
def sin():
return stdin.readline().rstrip()
def listInput():
return list(map(int, sin().split()))
def printBS(li):
if not li:
return
for i in range(len(li) - 1):
stdout.write("%d " % li[i])
stdout.write("%d\n" % li[-1])
def rev(a, k):
b = 0
for _ in range(k):
b = 2 * b + a % 2
a = a // 2
return b
t = int(sin())
for _ in range(t):
n, s = sin().split()
n = int(n)
r = 2**n
s1 = ["" for i in range(r)]
for i in range(r):
s1[rev(i, n)] = s[i]
print("".join(s1)) | FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR RETURN FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR STRING VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
The Chef has prepared the appetizers in the shapes of letters to spell a special message for the guests. There are n appetizers numbered from 0 to n-1 such that if the appetizers are arrayed in this order, they will display the message. The Chef plans to display them in this order on a table that can be viewed by all guests as they enter. The appetizers will only be served once all guests are seated.
The appetizers are not necessarily finished in the same order as they are numbered. So, when an appetizer is finished the Chef will write the number on a piece of paper and place it beside the appetizer on a counter between the kitchen and the restaurant. A server will retrieve this appetizer and place it in the proper location according to the number written beside it.
The Chef has a penchant for binary numbers. The number of appetizers created is a power of 2, say n = 2^{k}. Furthermore, he has written the number of the appetizer in binary with exactly k bits. That is, binary numbers with fewer than k bits are padded on the left with zeros so they are written with exactly k bits.
Unfortunately, this has unforseen complications. A binary number still "looks" binary when it is written upside down. For example, the binary number "0101" looks like "1010" when read upside down and the binary number "110" looks like "011" (the Chef uses simple vertical lines to denote a 1 bit). The Chef didn't realize that the servers would read the numbers upside down so he doesn't rotate the paper when he places it on the counter. Thus, when the server picks up an appetizer they place it the location indexed by the binary number when it is read upside down.
You are given the message the chef intended to display and you are to display the message that will be displayed after the servers move all appetizers to their locations based on the binary numbers they read.
------ Input ------
The first line consists of a single integer T ≤ 25 indicating the number of test cases to follow. Each test case consists of a single line beginning with an integer 1 ≤ k ≤ 16 followed by a string of precisely 2^{k} characters. The integer and the string are separated by a single space. The string has no spaces and is composed only of lower case letters from a to z.
------ Output ------
For each test case you are to output the scrambled message on a single line.
----- Sample Input 1 ------
2
2 chef
4 enjoyourapplepie
----- Sample Output 1 ------
cehf
eayejpuinpopolre | t = int(input())
for _ in range(t):
k, s = input().rstrip().split()
k = int(k)
d = []
for i, c in enumerate(s):
b = bin(i)[2:].rjust(k, "0")[::-1]
d.append((int(b, base=2), c))
d = sorted(d)
print("".join([c for i, c in d])) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR STRING NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR VAR VAR |
The Chef has prepared the appetizers in the shapes of letters to spell a special message for the guests. There are n appetizers numbered from 0 to n-1 such that if the appetizers are arrayed in this order, they will display the message. The Chef plans to display them in this order on a table that can be viewed by all guests as they enter. The appetizers will only be served once all guests are seated.
The appetizers are not necessarily finished in the same order as they are numbered. So, when an appetizer is finished the Chef will write the number on a piece of paper and place it beside the appetizer on a counter between the kitchen and the restaurant. A server will retrieve this appetizer and place it in the proper location according to the number written beside it.
The Chef has a penchant for binary numbers. The number of appetizers created is a power of 2, say n = 2^{k}. Furthermore, he has written the number of the appetizer in binary with exactly k bits. That is, binary numbers with fewer than k bits are padded on the left with zeros so they are written with exactly k bits.
Unfortunately, this has unforseen complications. A binary number still "looks" binary when it is written upside down. For example, the binary number "0101" looks like "1010" when read upside down and the binary number "110" looks like "011" (the Chef uses simple vertical lines to denote a 1 bit). The Chef didn't realize that the servers would read the numbers upside down so he doesn't rotate the paper when he places it on the counter. Thus, when the server picks up an appetizer they place it the location indexed by the binary number when it is read upside down.
You are given the message the chef intended to display and you are to display the message that will be displayed after the servers move all appetizers to their locations based on the binary numbers they read.
------ Input ------
The first line consists of a single integer T ≤ 25 indicating the number of test cases to follow. Each test case consists of a single line beginning with an integer 1 ≤ k ≤ 16 followed by a string of precisely 2^{k} characters. The integer and the string are separated by a single space. The string has no spaces and is composed only of lower case letters from a to z.
------ Output ------
For each test case you are to output the scrambled message on a single line.
----- Sample Input 1 ------
2
2 chef
4 enjoyourapplepie
----- Sample Output 1 ------
cehf
eayejpuinpopolre | for _ in range(int(input())):
k, s = input().split()
k = int(k)
n = 2**k
ans = [0] * n
for i in range(n):
ans[int(bin(i)[2:].zfill(k)[::-1], 2)] = s[i]
print("".join(ans)) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
The Chef has prepared the appetizers in the shapes of letters to spell a special message for the guests. There are n appetizers numbered from 0 to n-1 such that if the appetizers are arrayed in this order, they will display the message. The Chef plans to display them in this order on a table that can be viewed by all guests as they enter. The appetizers will only be served once all guests are seated.
The appetizers are not necessarily finished in the same order as they are numbered. So, when an appetizer is finished the Chef will write the number on a piece of paper and place it beside the appetizer on a counter between the kitchen and the restaurant. A server will retrieve this appetizer and place it in the proper location according to the number written beside it.
The Chef has a penchant for binary numbers. The number of appetizers created is a power of 2, say n = 2^{k}. Furthermore, he has written the number of the appetizer in binary with exactly k bits. That is, binary numbers with fewer than k bits are padded on the left with zeros so they are written with exactly k bits.
Unfortunately, this has unforseen complications. A binary number still "looks" binary when it is written upside down. For example, the binary number "0101" looks like "1010" when read upside down and the binary number "110" looks like "011" (the Chef uses simple vertical lines to denote a 1 bit). The Chef didn't realize that the servers would read the numbers upside down so he doesn't rotate the paper when he places it on the counter. Thus, when the server picks up an appetizer they place it the location indexed by the binary number when it is read upside down.
You are given the message the chef intended to display and you are to display the message that will be displayed after the servers move all appetizers to their locations based on the binary numbers they read.
------ Input ------
The first line consists of a single integer T ≤ 25 indicating the number of test cases to follow. Each test case consists of a single line beginning with an integer 1 ≤ k ≤ 16 followed by a string of precisely 2^{k} characters. The integer and the string are separated by a single space. The string has no spaces and is composed only of lower case letters from a to z.
------ Output ------
For each test case you are to output the scrambled message on a single line.
----- Sample Input 1 ------
2
2 chef
4 enjoyourapplepie
----- Sample Output 1 ------
cehf
eayejpuinpopolre | tests = int(input())
for i in range(tests):
num, string = input().split()
num = int(num)
arr = [0] * 2**num
arr[0] = string[0]
for j in range(2**num):
pos = j
ans = 0
count = 0
while j != 0:
if j & 1:
ans |= 1 << num - 1 - count
count += 1
j >>= 1
arr[ans] = string[pos]
print("".join(arr)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
The Chef has prepared the appetizers in the shapes of letters to spell a special message for the guests. There are n appetizers numbered from 0 to n-1 such that if the appetizers are arrayed in this order, they will display the message. The Chef plans to display them in this order on a table that can be viewed by all guests as they enter. The appetizers will only be served once all guests are seated.
The appetizers are not necessarily finished in the same order as they are numbered. So, when an appetizer is finished the Chef will write the number on a piece of paper and place it beside the appetizer on a counter between the kitchen and the restaurant. A server will retrieve this appetizer and place it in the proper location according to the number written beside it.
The Chef has a penchant for binary numbers. The number of appetizers created is a power of 2, say n = 2^{k}. Furthermore, he has written the number of the appetizer in binary with exactly k bits. That is, binary numbers with fewer than k bits are padded on the left with zeros so they are written with exactly k bits.
Unfortunately, this has unforseen complications. A binary number still "looks" binary when it is written upside down. For example, the binary number "0101" looks like "1010" when read upside down and the binary number "110" looks like "011" (the Chef uses simple vertical lines to denote a 1 bit). The Chef didn't realize that the servers would read the numbers upside down so he doesn't rotate the paper when he places it on the counter. Thus, when the server picks up an appetizer they place it the location indexed by the binary number when it is read upside down.
You are given the message the chef intended to display and you are to display the message that will be displayed after the servers move all appetizers to their locations based on the binary numbers they read.
------ Input ------
The first line consists of a single integer T ≤ 25 indicating the number of test cases to follow. Each test case consists of a single line beginning with an integer 1 ≤ k ≤ 16 followed by a string of precisely 2^{k} characters. The integer and the string are separated by a single space. The string has no spaces and is composed only of lower case letters from a to z.
------ Output ------
For each test case you are to output the scrambled message on a single line.
----- Sample Input 1 ------
2
2 chef
4 enjoyourapplepie
----- Sample Output 1 ------
cehf
eayejpuinpopolre | t = int(input())
for i in range(t):
n, s = map(str, input().split())
n = int(n)
l = list(s)
for j in range(len(s)):
z = bin(j)[2:]
z = int(("0" * (n - len(z)) + z)[::-1], 2)
l[z] = s[j]
print("".join(l)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
The Chef has prepared the appetizers in the shapes of letters to spell a special message for the guests. There are n appetizers numbered from 0 to n-1 such that if the appetizers are arrayed in this order, they will display the message. The Chef plans to display them in this order on a table that can be viewed by all guests as they enter. The appetizers will only be served once all guests are seated.
The appetizers are not necessarily finished in the same order as they are numbered. So, when an appetizer is finished the Chef will write the number on a piece of paper and place it beside the appetizer on a counter between the kitchen and the restaurant. A server will retrieve this appetizer and place it in the proper location according to the number written beside it.
The Chef has a penchant for binary numbers. The number of appetizers created is a power of 2, say n = 2^{k}. Furthermore, he has written the number of the appetizer in binary with exactly k bits. That is, binary numbers with fewer than k bits are padded on the left with zeros so they are written with exactly k bits.
Unfortunately, this has unforseen complications. A binary number still "looks" binary when it is written upside down. For example, the binary number "0101" looks like "1010" when read upside down and the binary number "110" looks like "011" (the Chef uses simple vertical lines to denote a 1 bit). The Chef didn't realize that the servers would read the numbers upside down so he doesn't rotate the paper when he places it on the counter. Thus, when the server picks up an appetizer they place it the location indexed by the binary number when it is read upside down.
You are given the message the chef intended to display and you are to display the message that will be displayed after the servers move all appetizers to their locations based on the binary numbers they read.
------ Input ------
The first line consists of a single integer T ≤ 25 indicating the number of test cases to follow. Each test case consists of a single line beginning with an integer 1 ≤ k ≤ 16 followed by a string of precisely 2^{k} characters. The integer and the string are separated by a single space. The string has no spaces and is composed only of lower case letters from a to z.
------ Output ------
For each test case you are to output the scrambled message on a single line.
----- Sample Input 1 ------
2
2 chef
4 enjoyourapplepie
----- Sample Output 1 ------
cehf
eayejpuinpopolre | for i in range(int(input())):
k, t = input().strip().split()
k = int(k)
l = len(t)
a = [" " for _ in range(l)]
for i in range(l):
s = str(bin(i))[2:]
s = "0" * (k - len(s)) + s
j = int(s[::-1], 2)
a[j] = t[i]
print("".join(a)) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
The Chef has prepared the appetizers in the shapes of letters to spell a special message for the guests. There are n appetizers numbered from 0 to n-1 such that if the appetizers are arrayed in this order, they will display the message. The Chef plans to display them in this order on a table that can be viewed by all guests as they enter. The appetizers will only be served once all guests are seated.
The appetizers are not necessarily finished in the same order as they are numbered. So, when an appetizer is finished the Chef will write the number on a piece of paper and place it beside the appetizer on a counter between the kitchen and the restaurant. A server will retrieve this appetizer and place it in the proper location according to the number written beside it.
The Chef has a penchant for binary numbers. The number of appetizers created is a power of 2, say n = 2^{k}. Furthermore, he has written the number of the appetizer in binary with exactly k bits. That is, binary numbers with fewer than k bits are padded on the left with zeros so they are written with exactly k bits.
Unfortunately, this has unforseen complications. A binary number still "looks" binary when it is written upside down. For example, the binary number "0101" looks like "1010" when read upside down and the binary number "110" looks like "011" (the Chef uses simple vertical lines to denote a 1 bit). The Chef didn't realize that the servers would read the numbers upside down so he doesn't rotate the paper when he places it on the counter. Thus, when the server picks up an appetizer they place it the location indexed by the binary number when it is read upside down.
You are given the message the chef intended to display and you are to display the message that will be displayed after the servers move all appetizers to their locations based on the binary numbers they read.
------ Input ------
The first line consists of a single integer T ≤ 25 indicating the number of test cases to follow. Each test case consists of a single line beginning with an integer 1 ≤ k ≤ 16 followed by a string of precisely 2^{k} characters. The integer and the string are separated by a single space. The string has no spaces and is composed only of lower case letters from a to z.
------ Output ------
For each test case you are to output the scrambled message on a single line.
----- Sample Input 1 ------
2
2 chef
4 enjoyourapplepie
----- Sample Output 1 ------
cehf
eayejpuinpopolre | for _ in range(int(input())):
k, l = map(str, input().split())
main = list(l)
nn = []
i = 0
while i < len(main):
f = list(str(bin(i)[2:].zfill(int(k))))
f.reverse()
h = "".join(f)
p = int(h, 2)
nn.append(main[p])
i += 1
print("".join(nn)) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
The Chef has prepared the appetizers in the shapes of letters to spell a special message for the guests. There are n appetizers numbered from 0 to n-1 such that if the appetizers are arrayed in this order, they will display the message. The Chef plans to display them in this order on a table that can be viewed by all guests as they enter. The appetizers will only be served once all guests are seated.
The appetizers are not necessarily finished in the same order as they are numbered. So, when an appetizer is finished the Chef will write the number on a piece of paper and place it beside the appetizer on a counter between the kitchen and the restaurant. A server will retrieve this appetizer and place it in the proper location according to the number written beside it.
The Chef has a penchant for binary numbers. The number of appetizers created is a power of 2, say n = 2^{k}. Furthermore, he has written the number of the appetizer in binary with exactly k bits. That is, binary numbers with fewer than k bits are padded on the left with zeros so they are written with exactly k bits.
Unfortunately, this has unforseen complications. A binary number still "looks" binary when it is written upside down. For example, the binary number "0101" looks like "1010" when read upside down and the binary number "110" looks like "011" (the Chef uses simple vertical lines to denote a 1 bit). The Chef didn't realize that the servers would read the numbers upside down so he doesn't rotate the paper when he places it on the counter. Thus, when the server picks up an appetizer they place it the location indexed by the binary number when it is read upside down.
You are given the message the chef intended to display and you are to display the message that will be displayed after the servers move all appetizers to their locations based on the binary numbers they read.
------ Input ------
The first line consists of a single integer T ≤ 25 indicating the number of test cases to follow. Each test case consists of a single line beginning with an integer 1 ≤ k ≤ 16 followed by a string of precisely 2^{k} characters. The integer and the string are separated by a single space. The string has no spaces and is composed only of lower case letters from a to z.
------ Output ------
For each test case you are to output the scrambled message on a single line.
----- Sample Input 1 ------
2
2 chef
4 enjoyourapplepie
----- Sample Output 1 ------
cehf
eayejpuinpopolre | def function(K, A):
arr = [(0) for i in range(len(A))]
for i in range(len(A)):
data = A[i]
value = K
num = i
res = 0
while num:
res = res << 1
res += num & 1
num = num >> 1
value -= 1
if value > 0:
res = res << value
arr[res] = data
return "".join(arr)
def main():
T = int(input())
for _ in range(T):
A = input().split(" ")
N = int(A[0])
S = A[1]
val = function(N, S)
print(val)
main() | FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR RETURN FUNC_CALL STRING VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
The Chef has prepared the appetizers in the shapes of letters to spell a special message for the guests. There are n appetizers numbered from 0 to n-1 such that if the appetizers are arrayed in this order, they will display the message. The Chef plans to display them in this order on a table that can be viewed by all guests as they enter. The appetizers will only be served once all guests are seated.
The appetizers are not necessarily finished in the same order as they are numbered. So, when an appetizer is finished the Chef will write the number on a piece of paper and place it beside the appetizer on a counter between the kitchen and the restaurant. A server will retrieve this appetizer and place it in the proper location according to the number written beside it.
The Chef has a penchant for binary numbers. The number of appetizers created is a power of 2, say n = 2^{k}. Furthermore, he has written the number of the appetizer in binary with exactly k bits. That is, binary numbers with fewer than k bits are padded on the left with zeros so they are written with exactly k bits.
Unfortunately, this has unforseen complications. A binary number still "looks" binary when it is written upside down. For example, the binary number "0101" looks like "1010" when read upside down and the binary number "110" looks like "011" (the Chef uses simple vertical lines to denote a 1 bit). The Chef didn't realize that the servers would read the numbers upside down so he doesn't rotate the paper when he places it on the counter. Thus, when the server picks up an appetizer they place it the location indexed by the binary number when it is read upside down.
You are given the message the chef intended to display and you are to display the message that will be displayed after the servers move all appetizers to their locations based on the binary numbers they read.
------ Input ------
The first line consists of a single integer T ≤ 25 indicating the number of test cases to follow. Each test case consists of a single line beginning with an integer 1 ≤ k ≤ 16 followed by a string of precisely 2^{k} characters. The integer and the string are separated by a single space. The string has no spaces and is composed only of lower case letters from a to z.
------ Output ------
For each test case you are to output the scrambled message on a single line.
----- Sample Input 1 ------
2
2 chef
4 enjoyourapplepie
----- Sample Output 1 ------
cehf
eayejpuinpopolre | def rev(i, val):
a = i
arr = []
while a > 0:
arr.append(a % 2)
a //= 2
while len(arr) < val:
arr.append(0)
change = 0
for i in range(val):
change += 2 ** (val - 1 - i) * arr[i]
return change
t = int(input())
for _ in range(t):
val, st = input().split()
val = int(val)
check = {}
arr = ["-1" for i in range(2**val)]
for i in range(2**val):
check[i] = 0
for i in range(2**val):
b = rev(i, val)
if check[i] == 0 and check[b] == 0:
check[i], check[b] = 1, 1
arr[i], arr[b] = st[b], st[i]
res = ""
for ele in arr:
res += ele
print(res) | FUNC_DEF ASSIGN VAR VAR ASSIGN VAR LIST WHILE VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER WHILE FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR STRING VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR STRING FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
The Chef has prepared the appetizers in the shapes of letters to spell a special message for the guests. There are n appetizers numbered from 0 to n-1 such that if the appetizers are arrayed in this order, they will display the message. The Chef plans to display them in this order on a table that can be viewed by all guests as they enter. The appetizers will only be served once all guests are seated.
The appetizers are not necessarily finished in the same order as they are numbered. So, when an appetizer is finished the Chef will write the number on a piece of paper and place it beside the appetizer on a counter between the kitchen and the restaurant. A server will retrieve this appetizer and place it in the proper location according to the number written beside it.
The Chef has a penchant for binary numbers. The number of appetizers created is a power of 2, say n = 2^{k}. Furthermore, he has written the number of the appetizer in binary with exactly k bits. That is, binary numbers with fewer than k bits are padded on the left with zeros so they are written with exactly k bits.
Unfortunately, this has unforseen complications. A binary number still "looks" binary when it is written upside down. For example, the binary number "0101" looks like "1010" when read upside down and the binary number "110" looks like "011" (the Chef uses simple vertical lines to denote a 1 bit). The Chef didn't realize that the servers would read the numbers upside down so he doesn't rotate the paper when he places it on the counter. Thus, when the server picks up an appetizer they place it the location indexed by the binary number when it is read upside down.
You are given the message the chef intended to display and you are to display the message that will be displayed after the servers move all appetizers to their locations based on the binary numbers they read.
------ Input ------
The first line consists of a single integer T ≤ 25 indicating the number of test cases to follow. Each test case consists of a single line beginning with an integer 1 ≤ k ≤ 16 followed by a string of precisely 2^{k} characters. The integer and the string are separated by a single space. The string has no spaces and is composed only of lower case letters from a to z.
------ Output ------
For each test case you are to output the scrambled message on a single line.
----- Sample Input 1 ------
2
2 chef
4 enjoyourapplepie
----- Sample Output 1 ------
cehf
eayejpuinpopolre | def masking(bits, n):
a = 1 << n - 1
ret = 0
mask = 1
i = 0
while i <= n - 1:
mask = 1 << i
a = 1 << n - i - 1
if bits & mask:
ret = ret | a
i += 1
return ret
for t in range(int(input())):
n, var = map(str, input().strip().split())
n = int(n)
var = list(var)
st = [0] * 2**n
for i in range(2**n):
c = masking(i, n)
if st[c] == 0:
st[c] = var[i]
print("".join(st)) | FUNC_DEF ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
The Chef has prepared the appetizers in the shapes of letters to spell a special message for the guests. There are n appetizers numbered from 0 to n-1 such that if the appetizers are arrayed in this order, they will display the message. The Chef plans to display them in this order on a table that can be viewed by all guests as they enter. The appetizers will only be served once all guests are seated.
The appetizers are not necessarily finished in the same order as they are numbered. So, when an appetizer is finished the Chef will write the number on a piece of paper and place it beside the appetizer on a counter between the kitchen and the restaurant. A server will retrieve this appetizer and place it in the proper location according to the number written beside it.
The Chef has a penchant for binary numbers. The number of appetizers created is a power of 2, say n = 2^{k}. Furthermore, he has written the number of the appetizer in binary with exactly k bits. That is, binary numbers with fewer than k bits are padded on the left with zeros so they are written with exactly k bits.
Unfortunately, this has unforseen complications. A binary number still "looks" binary when it is written upside down. For example, the binary number "0101" looks like "1010" when read upside down and the binary number "110" looks like "011" (the Chef uses simple vertical lines to denote a 1 bit). The Chef didn't realize that the servers would read the numbers upside down so he doesn't rotate the paper when he places it on the counter. Thus, when the server picks up an appetizer they place it the location indexed by the binary number when it is read upside down.
You are given the message the chef intended to display and you are to display the message that will be displayed after the servers move all appetizers to their locations based on the binary numbers they read.
------ Input ------
The first line consists of a single integer T ≤ 25 indicating the number of test cases to follow. Each test case consists of a single line beginning with an integer 1 ≤ k ≤ 16 followed by a string of precisely 2^{k} characters. The integer and the string are separated by a single space. The string has no spaces and is composed only of lower case letters from a to z.
------ Output ------
For each test case you are to output the scrambled message on a single line.
----- Sample Input 1 ------
2
2 chef
4 enjoyourapplepie
----- Sample Output 1 ------
cehf
eayejpuinpopolre | def decimalToBinary(n, k):
b = bin(n).replace("0b", "")
if len(b) < k:
return (k - len(b)) * "0" + b
return b
def binaryToDecimal(n):
return int(n, 2)
for _ in range(int(input())):
l = []
k, s = input().split(" ")
k = int(k)
d = {}
final = ""
for num in range(0, 2**k):
l.append(decimalToBinary(num, k))
for i in range(len(l)):
ele = l[i]
l[i] = ele[::-1]
for i in range(len(s)):
d[binaryToDecimal(l[i])] = s[i]
for i in range(len(d)):
final += d[i]
print(final) | FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING IF FUNC_CALL VAR VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR STRING VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
The Chef has prepared the appetizers in the shapes of letters to spell a special message for the guests. There are n appetizers numbered from 0 to n-1 such that if the appetizers are arrayed in this order, they will display the message. The Chef plans to display them in this order on a table that can be viewed by all guests as they enter. The appetizers will only be served once all guests are seated.
The appetizers are not necessarily finished in the same order as they are numbered. So, when an appetizer is finished the Chef will write the number on a piece of paper and place it beside the appetizer on a counter between the kitchen and the restaurant. A server will retrieve this appetizer and place it in the proper location according to the number written beside it.
The Chef has a penchant for binary numbers. The number of appetizers created is a power of 2, say n = 2^{k}. Furthermore, he has written the number of the appetizer in binary with exactly k bits. That is, binary numbers with fewer than k bits are padded on the left with zeros so they are written with exactly k bits.
Unfortunately, this has unforseen complications. A binary number still "looks" binary when it is written upside down. For example, the binary number "0101" looks like "1010" when read upside down and the binary number "110" looks like "011" (the Chef uses simple vertical lines to denote a 1 bit). The Chef didn't realize that the servers would read the numbers upside down so he doesn't rotate the paper when he places it on the counter. Thus, when the server picks up an appetizer they place it the location indexed by the binary number when it is read upside down.
You are given the message the chef intended to display and you are to display the message that will be displayed after the servers move all appetizers to their locations based on the binary numbers they read.
------ Input ------
The first line consists of a single integer T ≤ 25 indicating the number of test cases to follow. Each test case consists of a single line beginning with an integer 1 ≤ k ≤ 16 followed by a string of precisely 2^{k} characters. The integer and the string are separated by a single space. The string has no spaces and is composed only of lower case letters from a to z.
------ Output ------
For each test case you are to output the scrambled message on a single line.
----- Sample Input 1 ------
2
2 chef
4 enjoyourapplepie
----- Sample Output 1 ------
cehf
eayejpuinpopolre | def calcindex(n, i):
sum = 0
count = 0
while i > 0:
if i % 2 == 1:
sum = sum + pow(2, n - count - 1)
i = i // 2
count += 1
return sum
def calc(n, s):
n = int(n)
marked = [(False) for i in range(2**n)]
s = list(s)
for i in range(2**n):
index = calcindex(n, i)
if not marked[i]:
s[index], s[i] = s[i], s[index]
marked[i] = True
marked[index] = True
print("".join(s))
t = int(input())
for i in range(t):
n, s = list(input().split())
calc(n, s) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR |
The Chef has prepared the appetizers in the shapes of letters to spell a special message for the guests. There are n appetizers numbered from 0 to n-1 such that if the appetizers are arrayed in this order, they will display the message. The Chef plans to display them in this order on a table that can be viewed by all guests as they enter. The appetizers will only be served once all guests are seated.
The appetizers are not necessarily finished in the same order as they are numbered. So, when an appetizer is finished the Chef will write the number on a piece of paper and place it beside the appetizer on a counter between the kitchen and the restaurant. A server will retrieve this appetizer and place it in the proper location according to the number written beside it.
The Chef has a penchant for binary numbers. The number of appetizers created is a power of 2, say n = 2^{k}. Furthermore, he has written the number of the appetizer in binary with exactly k bits. That is, binary numbers with fewer than k bits are padded on the left with zeros so they are written with exactly k bits.
Unfortunately, this has unforseen complications. A binary number still "looks" binary when it is written upside down. For example, the binary number "0101" looks like "1010" when read upside down and the binary number "110" looks like "011" (the Chef uses simple vertical lines to denote a 1 bit). The Chef didn't realize that the servers would read the numbers upside down so he doesn't rotate the paper when he places it on the counter. Thus, when the server picks up an appetizer they place it the location indexed by the binary number when it is read upside down.
You are given the message the chef intended to display and you are to display the message that will be displayed after the servers move all appetizers to their locations based on the binary numbers they read.
------ Input ------
The first line consists of a single integer T ≤ 25 indicating the number of test cases to follow. Each test case consists of a single line beginning with an integer 1 ≤ k ≤ 16 followed by a string of precisely 2^{k} characters. The integer and the string are separated by a single space. The string has no spaces and is composed only of lower case letters from a to z.
------ Output ------
For each test case you are to output the scrambled message on a single line.
----- Sample Input 1 ------
2
2 chef
4 enjoyourapplepie
----- Sample Output 1 ------
cehf
eayejpuinpopolre | def b(n, k):
s = ""
while n // 2 != 0:
s += str(n % 2)
n = n // 2
s += str(n)
if len(s) < k:
s = s + "0" * (k - len(s))
return s
t = int(input())
for i in range(t):
k, s = map(str, input().split())
k = int(k)
y = 2**k
ans = ["0"] * y
for j in range(y):
x = int(b(j, k), 2)
ans[x] = s[j]
for j in ans:
print(j, end="")
print() | FUNC_DEF ASSIGN VAR STRING WHILE BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP LIST STRING VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR |
Given n and k, Construct a palindrome of size n using the binary representation of the number k.To construct the palindrome you can use the binary number of size k as many times as you wish and also you can trim all the zeros from the end.The palindrome must always begin with 1 and contains the maximum number of zeros.
Example 1 :
Input : n = 5, k = 6
Output : 11011
Explanation: Binary representation of 6 is
110.After combining 110
twice and trimming the extra 0 in
the end we get 11011
Example 2:
Input: n = 5, k = 8
Output: 10001
Explanation: Binary representation of 8 is
1000.After combining 1000 twice and trimming
the extra 0 in the end we get 10001.
Your Task:
You don't need to read or print anything. Your task is to complete the function binaryPalindrome() that takes two integers n, k and return a string of size n.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
1 ≤ k ≤ 10^{5} | class Solution:
def dfs(self, u, connect_chars, visited):
visited[u] = 1
for v in connect_chars[u]:
if visited[v] == 0:
self.dfs(v, connect_chars, visited)
def binaryPalindrome(self, n, k):
pos_arr = [0] * n
for i in range(n):
pos_arr[i] = i % k
connect_chars = {u: [] for u in range(k)}
for i in range(n // 2):
u = pos_arr[i]
v = pos_arr[n - 1 - i]
connect_chars[u].append(v)
connect_chars[v].append(u)
visited = [0] * k
self.dfs(0, connect_chars, visited)
res = ""
for i in range(n):
if visited[pos_arr[i]] == 1:
res += "1"
else:
res += "0"
return res | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR STRING VAR STRING RETURN VAR |
Given n and k, Construct a palindrome of size n using the binary representation of the number k.To construct the palindrome you can use the binary number of size k as many times as you wish and also you can trim all the zeros from the end.The palindrome must always begin with 1 and contains the maximum number of zeros.
Example 1 :
Input : n = 5, k = 6
Output : 11011
Explanation: Binary representation of 6 is
110.After combining 110
twice and trimming the extra 0 in
the end we get 11011
Example 2:
Input: n = 5, k = 8
Output: 10001
Explanation: Binary representation of 8 is
1000.After combining 1000 twice and trimming
the extra 0 in the end we get 10001.
Your Task:
You don't need to read or print anything. Your task is to complete the function binaryPalindrome() that takes two integers n, k and return a string of size n.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
1 ≤ k ≤ 10^{5} | class Solution:
def binaryPalindrome(self, n, k):
def dfs(parent, ans, connectchars):
ans[parent] = 1
for i in connectchars[parent]:
if not ans[i]:
dfs(i, ans, connectchars)
arr = [0] * n
ans = [0] * n
connectchars = [set() for i in range(k)]
for i in range(n):
arr[i] = i % k
for i in range(int(n / 2)):
connectchars[arr[i]].add(arr[n - i - 1])
connectchars[arr[n - i - 1]].add(arr[i])
dfs(0, ans, connectchars)
return "".join([str(ans[arr[i]]) for i in range(n)]) | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR RETURN FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR |
Given n and k, Construct a palindrome of size n using the binary representation of the number k.To construct the palindrome you can use the binary number of size k as many times as you wish and also you can trim all the zeros from the end.The palindrome must always begin with 1 and contains the maximum number of zeros.
Example 1 :
Input : n = 5, k = 6
Output : 11011
Explanation: Binary representation of 6 is
110.After combining 110
twice and trimming the extra 0 in
the end we get 11011
Example 2:
Input: n = 5, k = 8
Output: 10001
Explanation: Binary representation of 8 is
1000.After combining 1000 twice and trimming
the extra 0 in the end we get 10001.
Your Task:
You don't need to read or print anything. Your task is to complete the function binaryPalindrome() that takes two integers n, k and return a string of size n.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
1 ≤ k ≤ 10^{5} | class Solution:
def binaryPalindrome(self, n, k):
arr = ["0"] * n
arr[0] = arr[-1] = "1"
for i in range(1, n):
if i % k == 0:
arr[i] = "1"
i = 0
j = n - 1
while i < j:
if arr[i] == "1":
arr[j] = "1"
elif arr[j] == "1":
arr[i] = "1"
i += 1
j -= 1
return "".join(arr) | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR NUMBER VAR NUMBER STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER VAR NUMBER RETURN FUNC_CALL STRING VAR |
Given n and k, Construct a palindrome of size n using the binary representation of the number k.To construct the palindrome you can use the binary number of size k as many times as you wish and also you can trim all the zeros from the end.The palindrome must always begin with 1 and contains the maximum number of zeros.
Example 1 :
Input : n = 5, k = 6
Output : 11011
Explanation: Binary representation of 6 is
110.After combining 110
twice and trimming the extra 0 in
the end we get 11011
Example 2:
Input: n = 5, k = 8
Output: 10001
Explanation: Binary representation of 8 is
1000.After combining 1000 twice and trimming
the extra 0 in the end we get 10001.
Your Task:
You don't need to read or print anything. Your task is to complete the function binaryPalindrome() that takes two integers n, k and return a string of size n.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
1 ≤ k ≤ 10^{5} | class Solution:
def dfs(self, temp, result, charsjoin):
result[temp] = 1
for i in range(len(charsjoin[temp])):
if not result[charsjoin[temp][i]]:
self.dfs(charsjoin[temp][i], result, charsjoin)
def binaryPalindrome(self, n, k):
result = [0] * n
arr = [0] * n
charsjoin = [[] for i in range(k)]
for i in range(n):
arr[i] = i % k
for i in range(int(n / 2)):
charsjoin[arr[i]].append(arr[n - i - 1])
charsjoin[arr[n - i - 1]].append(arr[i])
self.dfs(0, result, charsjoin)
ans = ""
for i in range(n):
ans += str(result[arr[i]])
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR |
Given n and k, Construct a palindrome of size n using the binary representation of the number k.To construct the palindrome you can use the binary number of size k as many times as you wish and also you can trim all the zeros from the end.The palindrome must always begin with 1 and contains the maximum number of zeros.
Example 1 :
Input : n = 5, k = 6
Output : 11011
Explanation: Binary representation of 6 is
110.After combining 110
twice and trimming the extra 0 in
the end we get 11011
Example 2:
Input: n = 5, k = 8
Output: 10001
Explanation: Binary representation of 8 is
1000.After combining 1000 twice and trimming
the extra 0 in the end we get 10001.
Your Task:
You don't need to read or print anything. Your task is to complete the function binaryPalindrome() that takes two integers n, k and return a string of size n.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
1 ≤ k ≤ 10^{5} | class Solution:
def dfs(self, parent, ans, connectChars):
ans[0] = 1
for linked_k_i in connectChars[0]:
ans[linked_k_i] = 1
def binaryPalindrome(self, n, k):
arr = [(0) for i in range(n)]
ans = [(0) for i in range(n)]
connectChars = [[] for i in range(k)]
for i in range(n):
arr[i] = i % k
for i in range(n):
connectChars[arr[i]].append(arr[n - i - 1])
connectChars[arr[n - i - 1]].append(arr[i])
self.dfs(0, ans, connectChars)
s = ""
for i in range(n):
if ans[arr[i]] == 1:
s = s + "1"
else:
s = s + "0"
return s | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER NUMBER FOR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR STRING RETURN VAR |
Given n and k, Construct a palindrome of size n using the binary representation of the number k.To construct the palindrome you can use the binary number of size k as many times as you wish and also you can trim all the zeros from the end.The palindrome must always begin with 1 and contains the maximum number of zeros.
Example 1 :
Input : n = 5, k = 6
Output : 11011
Explanation: Binary representation of 6 is
110.After combining 110
twice and trimming the extra 0 in
the end we get 11011
Example 2:
Input: n = 5, k = 8
Output: 10001
Explanation: Binary representation of 8 is
1000.After combining 1000 twice and trimming
the extra 0 in the end we get 10001.
Your Task:
You don't need to read or print anything. Your task is to complete the function binaryPalindrome() that takes two integers n, k and return a string of size n.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
1 ≤ k ≤ 10^{5} | class Solution:
def dfs(self, node, ans, adj):
ans[node] = 1
for k in adj[node]:
if not ans[k]:
self.dfs(k, ans, adj)
return None
def binaryPalindrome(self, n, k):
arr = [(i % k) for i in range(n)]
ans = [0] * n
adj = {}
for i in range(n):
adj[i] = []
for i in range(n // 2):
adj[arr[i]].append(arr[n - i - 1])
adj[arr[n - i - 1]].append(arr[i])
self.dfs(0, ans, adj)
res = ""
for i in range(n):
res += str(ans[arr[i]])
return res | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN NONE FUNC_DEF ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR |
Given n and k, Construct a palindrome of size n using the binary representation of the number k.To construct the palindrome you can use the binary number of size k as many times as you wish and also you can trim all the zeros from the end.The palindrome must always begin with 1 and contains the maximum number of zeros.
Example 1 :
Input : n = 5, k = 6
Output : 11011
Explanation: Binary representation of 6 is
110.After combining 110
twice and trimming the extra 0 in
the end we get 11011
Example 2:
Input: n = 5, k = 8
Output: 10001
Explanation: Binary representation of 8 is
1000.After combining 1000 twice and trimming
the extra 0 in the end we get 10001.
Your Task:
You don't need to read or print anything. Your task is to complete the function binaryPalindrome() that takes two integers n, k and return a string of size n.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
1 ≤ k ≤ 10^{5} | class Solution:
def binaryPalindrome(self, n, k):
output = ["0" for i in range(n)]
base = ["0" for i in range(k)]
out = 0
if k >= n:
output[0] = "1"
output[-1] = "1"
output = "".join(output)
return int(output)
else:
rem = n % k
if rem == 0:
base[0] = "1"
base[-1] = "1"
else:
base[0] = "1"
base[rem - 1] = "1"
count = 0
for i in range(n):
count = count % k
output[i] = base[count]
count += 1
out = "".join(output)
return int(out) | CLASS_DEF FUNC_DEF ASSIGN VAR STRING VAR FUNC_CALL VAR VAR ASSIGN VAR STRING VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER STRING ASSIGN VAR FUNC_CALL STRING VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR RETURN FUNC_CALL VAR VAR |
Given n and k, Construct a palindrome of size n using the binary representation of the number k.To construct the palindrome you can use the binary number of size k as many times as you wish and also you can trim all the zeros from the end.The palindrome must always begin with 1 and contains the maximum number of zeros.
Example 1 :
Input : n = 5, k = 6
Output : 11011
Explanation: Binary representation of 6 is
110.After combining 110
twice and trimming the extra 0 in
the end we get 11011
Example 2:
Input: n = 5, k = 8
Output: 10001
Explanation: Binary representation of 8 is
1000.After combining 1000 twice and trimming
the extra 0 in the end we get 10001.
Your Task:
You don't need to read or print anything. Your task is to complete the function binaryPalindrome() that takes two integers n, k and return a string of size n.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
1 ≤ k ≤ 10^{5} | class Solution:
def construct_answer(self, ans, arr, connected_zero_set):
for i in range(len(arr)):
if arr[i] in connected_zero_set:
ans[i] = 1
return "".join(str(element) for element in ans)
def binaryPalindrome(self, n, k):
arr = [(0) for i in range(n)]
ans = [(0) for i in range(n)]
connected_zero_set = set([0])
for i in range(n):
arr[i] = i % k
for i in range(n):
if arr[i] in connected_zero_set:
connected_zero_set.add(arr[-i - 1])
if arr[-i - 1] in connected_zero_set:
connected_zero_set.add(arr[i])
return self.construct_answer(ans, arr, connected_zero_set) | CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR NUMBER RETURN FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR |
Given n and k, Construct a palindrome of size n using the binary representation of the number k.To construct the palindrome you can use the binary number of size k as many times as you wish and also you can trim all the zeros from the end.The palindrome must always begin with 1 and contains the maximum number of zeros.
Example 1 :
Input : n = 5, k = 6
Output : 11011
Explanation: Binary representation of 6 is
110.After combining 110
twice and trimming the extra 0 in
the end we get 11011
Example 2:
Input: n = 5, k = 8
Output: 10001
Explanation: Binary representation of 8 is
1000.After combining 1000 twice and trimming
the extra 0 in the end we get 10001.
Your Task:
You don't need to read or print anything. Your task is to complete the function binaryPalindrome() that takes two integers n, k and return a string of size n.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
1 ≤ k ≤ 10^{5} | class Solution:
def binaryPalindrome(self, n, k):
ans = [0] * n
arr = [0] * n
graph = [set() for i in range(k)]
for i in range(n):
arr[i] = i % k
for i in range(n // 2):
graph[arr[i]].add(arr[n - i - 1])
graph[arr[n - i - 1]].add(arr[i])
def dfs(node):
ans[node] = 1
for child in graph[node]:
if ans[child] == 0:
dfs(child)
dfs(0)
return "".join([str(ans[arr[i]]) for i in range(n)]) | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR |
Given n and k, Construct a palindrome of size n using the binary representation of the number k.To construct the palindrome you can use the binary number of size k as many times as you wish and also you can trim all the zeros from the end.The palindrome must always begin with 1 and contains the maximum number of zeros.
Example 1 :
Input : n = 5, k = 6
Output : 11011
Explanation: Binary representation of 6 is
110.After combining 110
twice and trimming the extra 0 in
the end we get 11011
Example 2:
Input: n = 5, k = 8
Output: 10001
Explanation: Binary representation of 8 is
1000.After combining 1000 twice and trimming
the extra 0 in the end we get 10001.
Your Task:
You don't need to read or print anything. Your task is to complete the function binaryPalindrome() that takes two integers n, k and return a string of size n.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
1 ≤ k ≤ 10^{5} | class Solution:
def binaryPalindrome(self, n, k):
a = [0] * n
r = []
c = [[] for i in range(k)]
if k == 1:
return str(k) * n
for i in range(n):
a[i] = i % k
t = [0] * k
for i in range(int(n / 2)):
c[a[i]].append(a[n - i - 1])
c[a[n - i - 1]].append(a[i])
for i in range(n // k + 1):
self.dfs(0, t, c)
r.extend(t)
t = [0] * k
r = r[:n]
for i in range(n):
r[i] = str(r[i])
return "".join(r)
def dfs(self, p, ans, c):
ans[p] = 1
for i in range(len(c[p])):
if ans[c[p][i]] != 1:
self.dfs(c[p][i], ans, c) | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST ASSIGN VAR LIST VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL STRING VAR FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR |
Given n and k, Construct a palindrome of size n using the binary representation of the number k.To construct the palindrome you can use the binary number of size k as many times as you wish and also you can trim all the zeros from the end.The palindrome must always begin with 1 and contains the maximum number of zeros.
Example 1 :
Input : n = 5, k = 6
Output : 11011
Explanation: Binary representation of 6 is
110.After combining 110
twice and trimming the extra 0 in
the end we get 11011
Example 2:
Input: n = 5, k = 8
Output: 10001
Explanation: Binary representation of 8 is
1000.After combining 1000 twice and trimming
the extra 0 in the end we get 10001.
Your Task:
You don't need to read or print anything. Your task is to complete the function binaryPalindrome() that takes two integers n, k and return a string of size n.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
1 ≤ k ≤ 10^{5} | class Solution:
def binaryPalindrome(self, n, k):
arr = [("0" if i % k else "1") for i in range(n)]
for i in range(n):
if arr[i] == "1":
arr[n - i - 1] = "1"
output = ""
for i in arr:
output += i
return output | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR VAR STRING STRING VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER STRING ASSIGN VAR STRING FOR VAR VAR VAR VAR RETURN VAR |
Given an array of integers arr[] of length N, every element appears thrice except for one which occurs once.
Find that element which occurs once.
Example 1:
Input:
N = 4
arr[] = {1, 10, 1, 1}
Output:
10
Explanation:
10 occurs once in the array while the other
element 1 occurs thrice.
Example 2:
Input:
N = 10
arr[] = {3, 2, 1, 34, 34, 1, 2, 34, 2, 1}
Output:
3
Explanation:
All elements except 3 occurs thrice in the array.
Your Task:
The task is to complete the function singleElement() which finds and returns the element occuring once in the array.
Constraints:
1 ≤ N ≤ 10^{6}
-10^{9} ≤ A[i] ≤ 10^{9}
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1). | class Solution:
def singleElement(self, arr, N):
arr = sorted(arr)
for i in range(len(arr)):
count = arr.count(arr[i])
if count == 1:
return arr[i] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN VAR VAR |
Given an array of integers arr[] of length N, every element appears thrice except for one which occurs once.
Find that element which occurs once.
Example 1:
Input:
N = 4
arr[] = {1, 10, 1, 1}
Output:
10
Explanation:
10 occurs once in the array while the other
element 1 occurs thrice.
Example 2:
Input:
N = 10
arr[] = {3, 2, 1, 34, 34, 1, 2, 34, 2, 1}
Output:
3
Explanation:
All elements except 3 occurs thrice in the array.
Your Task:
The task is to complete the function singleElement() which finds and returns the element occuring once in the array.
Constraints:
1 ≤ N ≤ 10^{6}
-10^{9} ≤ A[i] ≤ 10^{9}
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1). | class Solution:
def singleElement(self, arr, N):
h = {}
for num in arr:
h[num] = h.get(num, 0) + 1
for key in h:
if h[key] == 1:
return key | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR VAR NUMBER RETURN VAR |
Given an array of integers arr[] of length N, every element appears thrice except for one which occurs once.
Find that element which occurs once.
Example 1:
Input:
N = 4
arr[] = {1, 10, 1, 1}
Output:
10
Explanation:
10 occurs once in the array while the other
element 1 occurs thrice.
Example 2:
Input:
N = 10
arr[] = {3, 2, 1, 34, 34, 1, 2, 34, 2, 1}
Output:
3
Explanation:
All elements except 3 occurs thrice in the array.
Your Task:
The task is to complete the function singleElement() which finds and returns the element occuring once in the array.
Constraints:
1 ≤ N ≤ 10^{6}
-10^{9} ≤ A[i] ≤ 10^{9}
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1). | class Solution:
def singleElement(self, arr, N):
seen = {}
for x in arr:
if x not in seen:
seen[x] = 1
else:
seen[x] += 1
singleelem = {value: key for key, value in seen.items()}
return singleelem[1] | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR RETURN VAR NUMBER |
Given an array of integers arr[] of length N, every element appears thrice except for one which occurs once.
Find that element which occurs once.
Example 1:
Input:
N = 4
arr[] = {1, 10, 1, 1}
Output:
10
Explanation:
10 occurs once in the array while the other
element 1 occurs thrice.
Example 2:
Input:
N = 10
arr[] = {3, 2, 1, 34, 34, 1, 2, 34, 2, 1}
Output:
3
Explanation:
All elements except 3 occurs thrice in the array.
Your Task:
The task is to complete the function singleElement() which finds and returns the element occuring once in the array.
Constraints:
1 ≤ N ≤ 10^{6}
-10^{9} ≤ A[i] ≤ 10^{9}
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1). | class Solution:
def singleElement(self, arr, N):
ones = 0
twos = 0
for x in arr:
ones = (ones ^ x) & ~twos
twos = (twos ^ x) & ~ones
return ones | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR |
Given an array of integers arr[] of length N, every element appears thrice except for one which occurs once.
Find that element which occurs once.
Example 1:
Input:
N = 4
arr[] = {1, 10, 1, 1}
Output:
10
Explanation:
10 occurs once in the array while the other
element 1 occurs thrice.
Example 2:
Input:
N = 10
arr[] = {3, 2, 1, 34, 34, 1, 2, 34, 2, 1}
Output:
3
Explanation:
All elements except 3 occurs thrice in the array.
Your Task:
The task is to complete the function singleElement() which finds and returns the element occuring once in the array.
Constraints:
1 ≤ N ≤ 10^{6}
-10^{9} ≤ A[i] ≤ 10^{9}
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1). | class Solution:
def singleElement(self, arr, N):
sort = sorted(arr)
if N == 1:
return sort[0]
if sort[0] != sort[1]:
return sort[0]
for i in range(1, N):
if i == N - 1:
return sort[i]
if (sort[i - 1] != sort[i]) & (sort[i] != sort[i + 1]):
return sort[i] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER RETURN VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR |
Given an array of integers arr[] of length N, every element appears thrice except for one which occurs once.
Find that element which occurs once.
Example 1:
Input:
N = 4
arr[] = {1, 10, 1, 1}
Output:
10
Explanation:
10 occurs once in the array while the other
element 1 occurs thrice.
Example 2:
Input:
N = 10
arr[] = {3, 2, 1, 34, 34, 1, 2, 34, 2, 1}
Output:
3
Explanation:
All elements except 3 occurs thrice in the array.
Your Task:
The task is to complete the function singleElement() which finds and returns the element occuring once in the array.
Constraints:
1 ≤ N ≤ 10^{6}
-10^{9} ≤ A[i] ≤ 10^{9}
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1). | class Solution:
def singleElement(self, arr, N):
a = 0
b = 0
for i in range(N):
b = b ^ a & arr[i]
a = a ^ arr[i]
common = ~(a & b)
a = a & common
b = b & common
return a | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR |
Given an array of integers arr[] of length N, every element appears thrice except for one which occurs once.
Find that element which occurs once.
Example 1:
Input:
N = 4
arr[] = {1, 10, 1, 1}
Output:
10
Explanation:
10 occurs once in the array while the other
element 1 occurs thrice.
Example 2:
Input:
N = 10
arr[] = {3, 2, 1, 34, 34, 1, 2, 34, 2, 1}
Output:
3
Explanation:
All elements except 3 occurs thrice in the array.
Your Task:
The task is to complete the function singleElement() which finds and returns the element occuring once in the array.
Constraints:
1 ≤ N ≤ 10^{6}
-10^{9} ≤ A[i] ≤ 10^{9}
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1). | class Solution:
def singleElement(self, arr, N):
dic = {}
for i in arr:
if i not in dic:
dic[i] = 1
else:
dic[i] += 1
for key, value in dic.items():
if value == 1:
return key | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR IF VAR NUMBER RETURN VAR |
Given an array of integers arr[] of length N, every element appears thrice except for one which occurs once.
Find that element which occurs once.
Example 1:
Input:
N = 4
arr[] = {1, 10, 1, 1}
Output:
10
Explanation:
10 occurs once in the array while the other
element 1 occurs thrice.
Example 2:
Input:
N = 10
arr[] = {3, 2, 1, 34, 34, 1, 2, 34, 2, 1}
Output:
3
Explanation:
All elements except 3 occurs thrice in the array.
Your Task:
The task is to complete the function singleElement() which finds and returns the element occuring once in the array.
Constraints:
1 ≤ N ≤ 10^{6}
-10^{9} ≤ A[i] ≤ 10^{9}
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1). | class Solution:
def singleElement(self, arr, N):
count = 0
arr.sort()
for i in range(N - 1):
if arr[i] != arr[i + 1] and count == 0:
return arr[i]
else:
count += 1
if arr[i] != arr[i + 1]:
count = 0
return arr[-1] | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER RETURN VAR NUMBER |
Given an array of integers arr[] of length N, every element appears thrice except for one which occurs once.
Find that element which occurs once.
Example 1:
Input:
N = 4
arr[] = {1, 10, 1, 1}
Output:
10
Explanation:
10 occurs once in the array while the other
element 1 occurs thrice.
Example 2:
Input:
N = 10
arr[] = {3, 2, 1, 34, 34, 1, 2, 34, 2, 1}
Output:
3
Explanation:
All elements except 3 occurs thrice in the array.
Your Task:
The task is to complete the function singleElement() which finds and returns the element occuring once in the array.
Constraints:
1 ≤ N ≤ 10^{6}
-10^{9} ≤ A[i] ≤ 10^{9}
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1). | class Solution:
def singleElement(self, arr, N):
d = dict()
for i in arr:
m = 1
if i in d.keys():
m += d[i]
d[i] = m
for i in d.keys():
if d[i] == 1:
return i
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR IF VAR VAR NUMBER RETURN VAR RETURN NUMBER |
Given an array of integers arr[] of length N, every element appears thrice except for one which occurs once.
Find that element which occurs once.
Example 1:
Input:
N = 4
arr[] = {1, 10, 1, 1}
Output:
10
Explanation:
10 occurs once in the array while the other
element 1 occurs thrice.
Example 2:
Input:
N = 10
arr[] = {3, 2, 1, 34, 34, 1, 2, 34, 2, 1}
Output:
3
Explanation:
All elements except 3 occurs thrice in the array.
Your Task:
The task is to complete the function singleElement() which finds and returns the element occuring once in the array.
Constraints:
1 ≤ N ≤ 10^{6}
-10^{9} ≤ A[i] ≤ 10^{9}
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1). | class Solution:
def singleElement(self, arr, N):
arr.sort()
for i in range(1, N, 3):
if arr[i] != arr[i - 1]:
return arr[i - 1]
return arr[-1] | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR BIN_OP VAR NUMBER RETURN VAR NUMBER |
Given an array of integers arr[] of length N, every element appears thrice except for one which occurs once.
Find that element which occurs once.
Example 1:
Input:
N = 4
arr[] = {1, 10, 1, 1}
Output:
10
Explanation:
10 occurs once in the array while the other
element 1 occurs thrice.
Example 2:
Input:
N = 10
arr[] = {3, 2, 1, 34, 34, 1, 2, 34, 2, 1}
Output:
3
Explanation:
All elements except 3 occurs thrice in the array.
Your Task:
The task is to complete the function singleElement() which finds and returns the element occuring once in the array.
Constraints:
1 ≤ N ≤ 10^{6}
-10^{9} ≤ A[i] ≤ 10^{9}
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1). | class Solution:
def singleElement(self, arr, N):
ones = 0
twos = 0
for i in range(N):
current = arr[i]
twos |= ones & current
ones ^= current
common = ones & twos
ones &= ~common
twos &= ~common
return ones | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR RETURN VAR |
Given an array of integers arr[] of length N, every element appears thrice except for one which occurs once.
Find that element which occurs once.
Example 1:
Input:
N = 4
arr[] = {1, 10, 1, 1}
Output:
10
Explanation:
10 occurs once in the array while the other
element 1 occurs thrice.
Example 2:
Input:
N = 10
arr[] = {3, 2, 1, 34, 34, 1, 2, 34, 2, 1}
Output:
3
Explanation:
All elements except 3 occurs thrice in the array.
Your Task:
The task is to complete the function singleElement() which finds and returns the element occuring once in the array.
Constraints:
1 ≤ N ≤ 10^{6}
-10^{9} ≤ A[i] ≤ 10^{9}
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1). | class Solution:
def singleElement(self, arr, N):
a, b, c = -1, 0, 0
for r in arr:
a1, b1, c1 = a & r, b & r, c & r
a, b, c = a ^ a1, b ^ b1, c ^ c1
a, b, c = a | c1, b | a1, c | b1
return b | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR RETURN VAR |
Given an array of integers arr[] of length N, every element appears thrice except for one which occurs once.
Find that element which occurs once.
Example 1:
Input:
N = 4
arr[] = {1, 10, 1, 1}
Output:
10
Explanation:
10 occurs once in the array while the other
element 1 occurs thrice.
Example 2:
Input:
N = 10
arr[] = {3, 2, 1, 34, 34, 1, 2, 34, 2, 1}
Output:
3
Explanation:
All elements except 3 occurs thrice in the array.
Your Task:
The task is to complete the function singleElement() which finds and returns the element occuring once in the array.
Constraints:
1 ≤ N ≤ 10^{6}
-10^{9} ≤ A[i] ≤ 10^{9}
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1). | class Solution:
def singleElement(self, arr, N):
g = {}
for i in range(0, N):
c = arr[i]
if c not in g:
g[c] = 1
else:
g[c] = g[c] + 1
for x in g:
if g[x] == 1:
return x | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER RETURN VAR |
Given an array of integers arr[] of length N, every element appears thrice except for one which occurs once.
Find that element which occurs once.
Example 1:
Input:
N = 4
arr[] = {1, 10, 1, 1}
Output:
10
Explanation:
10 occurs once in the array while the other
element 1 occurs thrice.
Example 2:
Input:
N = 10
arr[] = {3, 2, 1, 34, 34, 1, 2, 34, 2, 1}
Output:
3
Explanation:
All elements except 3 occurs thrice in the array.
Your Task:
The task is to complete the function singleElement() which finds and returns the element occuring once in the array.
Constraints:
1 ≤ N ≤ 10^{6}
-10^{9} ≤ A[i] ≤ 10^{9}
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1). | class Solution:
def singleElement(self, arr, N):
d = {}
ans = 0
for i in arr:
if i not in d.keys():
d[i] = 1
else:
d[i] += 1
for i in list(d.keys()):
if d[i] == 1:
return i | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR IF VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR NUMBER RETURN VAR |
Given an array of integers arr[] of length N, every element appears thrice except for one which occurs once.
Find that element which occurs once.
Example 1:
Input:
N = 4
arr[] = {1, 10, 1, 1}
Output:
10
Explanation:
10 occurs once in the array while the other
element 1 occurs thrice.
Example 2:
Input:
N = 10
arr[] = {3, 2, 1, 34, 34, 1, 2, 34, 2, 1}
Output:
3
Explanation:
All elements except 3 occurs thrice in the array.
Your Task:
The task is to complete the function singleElement() which finds and returns the element occuring once in the array.
Constraints:
1 ≤ N ≤ 10^{6}
-10^{9} ≤ A[i] ≤ 10^{9}
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1). | class Solution:
def singleElement(self, arr, N):
sum1 = 0
for i in arr:
sum1 += i
set1 = set(arr)
set1 = sorted(set1)
sum2 = 0
for ele in set1:
sum2 += ele
return int(sum2 - (sum1 - sum2) / 2) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER |
Given an array of integers arr[] of length N, every element appears thrice except for one which occurs once.
Find that element which occurs once.
Example 1:
Input:
N = 4
arr[] = {1, 10, 1, 1}
Output:
10
Explanation:
10 occurs once in the array while the other
element 1 occurs thrice.
Example 2:
Input:
N = 10
arr[] = {3, 2, 1, 34, 34, 1, 2, 34, 2, 1}
Output:
3
Explanation:
All elements except 3 occurs thrice in the array.
Your Task:
The task is to complete the function singleElement() which finds and returns the element occuring once in the array.
Constraints:
1 ≤ N ≤ 10^{6}
-10^{9} ≤ A[i] ≤ 10^{9}
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1). | class Solution:
def singleElement(self, arr, N):
d = dict()
for ele in arr:
d[ele] = 0
for ele in arr:
d[ele] = d[ele] + 1
for ele in d.keys():
if d[ele] == 1:
return ele | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR IF VAR VAR NUMBER RETURN VAR |
Given an array of integers arr[] of length N, every element appears thrice except for one which occurs once.
Find that element which occurs once.
Example 1:
Input:
N = 4
arr[] = {1, 10, 1, 1}
Output:
10
Explanation:
10 occurs once in the array while the other
element 1 occurs thrice.
Example 2:
Input:
N = 10
arr[] = {3, 2, 1, 34, 34, 1, 2, 34, 2, 1}
Output:
3
Explanation:
All elements except 3 occurs thrice in the array.
Your Task:
The task is to complete the function singleElement() which finds and returns the element occuring once in the array.
Constraints:
1 ≤ N ≤ 10^{6}
-10^{9} ≤ A[i] ≤ 10^{9}
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1). | class Solution:
def singleElement(self, arr, N):
p = {}
for i in arr:
p[i] = 0
for i in range(N):
p[arr[i]] += 1
for i in p:
if p[i] == 1:
return i | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER RETURN VAR |
Given an array of integers arr[] of length N, every element appears thrice except for one which occurs once.
Find that element which occurs once.
Example 1:
Input:
N = 4
arr[] = {1, 10, 1, 1}
Output:
10
Explanation:
10 occurs once in the array while the other
element 1 occurs thrice.
Example 2:
Input:
N = 10
arr[] = {3, 2, 1, 34, 34, 1, 2, 34, 2, 1}
Output:
3
Explanation:
All elements except 3 occurs thrice in the array.
Your Task:
The task is to complete the function singleElement() which finds and returns the element occuring once in the array.
Constraints:
1 ≤ N ≤ 10^{6}
-10^{9} ≤ A[i] ≤ 10^{9}
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1). | class Solution:
def singleElement(self, arr, N):
ones = twos = 0
for i in range(N):
twos |= ones & arr[i]
ones ^= arr[i]
comm = ~(ones & twos)
ones &= comm
twos &= comm
return ones | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR RETURN VAR |
Given an array of integers arr[] of length N, every element appears thrice except for one which occurs once.
Find that element which occurs once.
Example 1:
Input:
N = 4
arr[] = {1, 10, 1, 1}
Output:
10
Explanation:
10 occurs once in the array while the other
element 1 occurs thrice.
Example 2:
Input:
N = 10
arr[] = {3, 2, 1, 34, 34, 1, 2, 34, 2, 1}
Output:
3
Explanation:
All elements except 3 occurs thrice in the array.
Your Task:
The task is to complete the function singleElement() which finds and returns the element occuring once in the array.
Constraints:
1 ≤ N ≤ 10^{6}
-10^{9} ≤ A[i] ≤ 10^{9}
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1). | class Solution:
def singleElement(self, a, n):
d = {}
for i in range(n):
if a[i] not in d.keys():
d[a[i]] = 1
else:
d[a[i]] += 1
for i in d.keys():
if d[i] == 1:
return i | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR IF VAR VAR NUMBER RETURN VAR |
Given an array of integers arr[] of length N, every element appears thrice except for one which occurs once.
Find that element which occurs once.
Example 1:
Input:
N = 4
arr[] = {1, 10, 1, 1}
Output:
10
Explanation:
10 occurs once in the array while the other
element 1 occurs thrice.
Example 2:
Input:
N = 10
arr[] = {3, 2, 1, 34, 34, 1, 2, 34, 2, 1}
Output:
3
Explanation:
All elements except 3 occurs thrice in the array.
Your Task:
The task is to complete the function singleElement() which finds and returns the element occuring once in the array.
Constraints:
1 ≤ N ≤ 10^{6}
-10^{9} ≤ A[i] ≤ 10^{9}
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1). | class Solution:
def singleElement(self, arr, N):
return (3 * sum(set(arr)) - sum(arr)) // 2
sums = [0] * 32
for a in arr:
for i in range(32):
if a >> i & 1:
sums[i] = (sums[i] + 1) % 3
output = 0
for i, b in enumerate(sums):
if b:
output += 1 << i
return output | CLASS_DEF FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP NUMBER VAR RETURN VAR |
Shreyansh has an integer N. He is really curious about the binary representation of integers. He sees that any given integer has a number of set bits. Now he wants to find out that how many positive integers, strictly less than N, have the same number of set bits as N.
He is a little weak in maths. Help him find the number of integers.
Note : Since N takes large values, brute force won't work.
Example 1:
Input:
N = 8
Output: 3
Explanation:
Binary representation of 8 : 1000
So the integers less than 8 with
same number of set bits are : 4, 2, 1
Hence, the result is 3.
Example 2:
Input:
N = 4
Output: 2
Explanation:
Binary representation of 4 : 100
So the integers less than 4 with
same number of set bits are : 2, 1
So, The result is 2.
Your Task:
You don't need to read input or print anything. Your task is to complete the function count() which takes the integer N as input parameters and returns the required answer.
Expected Time Complexity: O(log(n))
Expected Auxiliary Space: O(log(n)*log(n))
Constraints :
1 <= N <= 10^12 | def bit_count(N):
t = 0
while N > 0:
d = N % 2
N = int(N / 2)
if d == 1:
t += 1
return t
class Solution:
def ncr(self, n, r):
if r > n:
return 0
if r == 0 or r == n:
return 1
if self.cache[n][r] != -1:
return self.cache[n][r]
res = self.ncr(n - 1, r - 1) + self.ncr(n - 1, r)
self.cache[n][r] = res
return res
def count(self, N):
self.cache = [([-1] * 40) for _ in range(40)]
res = 0
ones = 0
bits = 0
while N != 0:
if N & 1:
res += self.ncr(bits, ones + 1)
ones += 1
bits += 1
N >>= 1
return res | FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR NUMBER VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR |
Shreyansh has an integer N. He is really curious about the binary representation of integers. He sees that any given integer has a number of set bits. Now he wants to find out that how many positive integers, strictly less than N, have the same number of set bits as N.
He is a little weak in maths. Help him find the number of integers.
Note : Since N takes large values, brute force won't work.
Example 1:
Input:
N = 8
Output: 3
Explanation:
Binary representation of 8 : 1000
So the integers less than 8 with
same number of set bits are : 4, 2, 1
Hence, the result is 3.
Example 2:
Input:
N = 4
Output: 2
Explanation:
Binary representation of 4 : 100
So the integers less than 4 with
same number of set bits are : 2, 1
So, The result is 2.
Your Task:
You don't need to read input or print anything. Your task is to complete the function count() which takes the integer N as input parameters and returns the required answer.
Expected Time Complexity: O(log(n))
Expected Auxiliary Space: O(log(n)*log(n))
Constraints :
1 <= N <= 10^12 | class Solution:
def fact(self, n):
res = 1
for i in range(2, n + 1):
res *= i
return res
def combi(self, n, r):
if n == 0 or n < r:
return 0
return self.fact(n) / self.fact(r) / self.fact(n - r)
def count(self, N):
st = bin(N)[2:]
res = 0
count1 = 0
l = len(st)
for i in range(l - 1, -1, -1):
if st[i] == "1":
count1 += 1
res += self.combi(l - i - 1, count1)
return round(res) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER VAR VAR RETURN NUMBER RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN FUNC_CALL VAR VAR |
Shreyansh has an integer N. He is really curious about the binary representation of integers. He sees that any given integer has a number of set bits. Now he wants to find out that how many positive integers, strictly less than N, have the same number of set bits as N.
He is a little weak in maths. Help him find the number of integers.
Note : Since N takes large values, brute force won't work.
Example 1:
Input:
N = 8
Output: 3
Explanation:
Binary representation of 8 : 1000
So the integers less than 8 with
same number of set bits are : 4, 2, 1
Hence, the result is 3.
Example 2:
Input:
N = 4
Output: 2
Explanation:
Binary representation of 4 : 100
So the integers less than 4 with
same number of set bits are : 2, 1
So, The result is 2.
Your Task:
You don't need to read input or print anything. Your task is to complete the function count() which takes the integer N as input parameters and returns the required answer.
Expected Time Complexity: O(log(n))
Expected Auxiliary Space: O(log(n)*log(n))
Constraints :
1 <= N <= 10^12 | import sys
sys.setrecursionlimit(10**8)
class Solution:
def count(self, N):
def fact(num):
if num == 1 or num == 0:
return 1
return num * fact(num - 1)
ans = 0
binary = str(bin(N))[2:]
n = len(binary)
count_one = 0
for i in binary:
if i == "1":
count_one += 1
for i in range(n - 1):
if binary[i] == "1":
empty_post = n - (i + 1)
if empty_post < count_one:
break
upper = fact(empty_post)
lower = fact(count_one) * fact(empty_post - count_one)
ans += upper / lower
count_one -= 1
return int(ans) | IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER RETURN BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR |
Shreyansh has an integer N. He is really curious about the binary representation of integers. He sees that any given integer has a number of set bits. Now he wants to find out that how many positive integers, strictly less than N, have the same number of set bits as N.
He is a little weak in maths. Help him find the number of integers.
Note : Since N takes large values, brute force won't work.
Example 1:
Input:
N = 8
Output: 3
Explanation:
Binary representation of 8 : 1000
So the integers less than 8 with
same number of set bits are : 4, 2, 1
Hence, the result is 3.
Example 2:
Input:
N = 4
Output: 2
Explanation:
Binary representation of 4 : 100
So the integers less than 4 with
same number of set bits are : 2, 1
So, The result is 2.
Your Task:
You don't need to read input or print anything. Your task is to complete the function count() which takes the integer N as input parameters and returns the required answer.
Expected Time Complexity: O(log(n))
Expected Auxiliary Space: O(log(n)*log(n))
Constraints :
1 <= N <= 10^12 | class Solution:
def nCr(self, n, r):
if r > n:
return 0
p = 1
for i in range(r):
p = p * n
n -= 1
q = r
for i in range(q):
p = p // r
r -= 1
return p
def count(self, N):
ones = 0
c = 0
ans = 0
while N != 0:
if N % 2 == 1:
ones += 1
ans += self.nCr(c, ones)
N = N // 2
c += 1
return ans | CLASS_DEF FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR |
Shreyansh has an integer N. He is really curious about the binary representation of integers. He sees that any given integer has a number of set bits. Now he wants to find out that how many positive integers, strictly less than N, have the same number of set bits as N.
He is a little weak in maths. Help him find the number of integers.
Note : Since N takes large values, brute force won't work.
Example 1:
Input:
N = 8
Output: 3
Explanation:
Binary representation of 8 : 1000
So the integers less than 8 with
same number of set bits are : 4, 2, 1
Hence, the result is 3.
Example 2:
Input:
N = 4
Output: 2
Explanation:
Binary representation of 4 : 100
So the integers less than 4 with
same number of set bits are : 2, 1
So, The result is 2.
Your Task:
You don't need to read input or print anything. Your task is to complete the function count() which takes the integer N as input parameters and returns the required answer.
Expected Time Complexity: O(log(n))
Expected Auxiliary Space: O(log(n)*log(n))
Constraints :
1 <= N <= 10^12 | class Solution:
def count(self, N):
ones, res = 0, 0
for i in range(40):
if N & 1 == 1:
ones += 1
res += self.C(i, ones)
N >>= 1
return res
def C(self, n, r):
if n < r:
return 0
if n == r or r == 0:
return 1
a = max(n - r, r)
res = n
for i in range(a + 1, n):
res *= i
return res // self.fac(n - r) if a == r else res // self.fac(r)
def fac(self, n):
return 1 if n <= 1 else n * self.fac(n - 1) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR FUNC_CALL VAR VAR FUNC_DEF RETURN VAR NUMBER NUMBER BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER |
Shreyansh has an integer N. He is really curious about the binary representation of integers. He sees that any given integer has a number of set bits. Now he wants to find out that how many positive integers, strictly less than N, have the same number of set bits as N.
He is a little weak in maths. Help him find the number of integers.
Note : Since N takes large values, brute force won't work.
Example 1:
Input:
N = 8
Output: 3
Explanation:
Binary representation of 8 : 1000
So the integers less than 8 with
same number of set bits are : 4, 2, 1
Hence, the result is 3.
Example 2:
Input:
N = 4
Output: 2
Explanation:
Binary representation of 4 : 100
So the integers less than 4 with
same number of set bits are : 2, 1
So, The result is 2.
Your Task:
You don't need to read input or print anything. Your task is to complete the function count() which takes the integer N as input parameters and returns the required answer.
Expected Time Complexity: O(log(n))
Expected Auxiliary Space: O(log(n)*log(n))
Constraints :
1 <= N <= 10^12 | class Solution:
def comb(self, n, r):
if r > n:
return 0
if n == 0:
return 0
if r == 0:
return 1
if r == 1:
return n
if r > n - r:
r = n - r
ans = 1
for i in range(1, r + 1):
ans *= n - r + i
ans //= i
return ans
def count(self, N):
bitPosition = 1
setBitsCount = 0
ans = 0
while N:
if N & 1:
setBitsCount += 1
ans += self.comb(bitPosition - 1, setBitsCount)
N >>= 1
bitPosition += 1
return ans | CLASS_DEF FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN VAR IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER RETURN VAR |
Shreyansh has an integer N. He is really curious about the binary representation of integers. He sees that any given integer has a number of set bits. Now he wants to find out that how many positive integers, strictly less than N, have the same number of set bits as N.
He is a little weak in maths. Help him find the number of integers.
Note : Since N takes large values, brute force won't work.
Example 1:
Input:
N = 8
Output: 3
Explanation:
Binary representation of 8 : 1000
So the integers less than 8 with
same number of set bits are : 4, 2, 1
Hence, the result is 3.
Example 2:
Input:
N = 4
Output: 2
Explanation:
Binary representation of 4 : 100
So the integers less than 4 with
same number of set bits are : 2, 1
So, The result is 2.
Your Task:
You don't need to read input or print anything. Your task is to complete the function count() which takes the integer N as input parameters and returns the required answer.
Expected Time Complexity: O(log(n))
Expected Auxiliary Space: O(log(n)*log(n))
Constraints :
1 <= N <= 10^12 | class Solution:
cache = [([-1] * 40) for _ in range(40)]
def count(self, N):
ones, res = 0, 0
for i in range(40):
if N & 1 == 1:
ones += 1
res += self.C(i, ones)
N >>= 1
return res
def C(self, n, r):
if n < r:
return 0
if n == r or r == 0:
return 1
if self.cache[n][r] != -1:
return self.cache[n][r]
res = self.C(n - 1, r - 1) + self.C(n - 1, r)
self.cache[n][r] = res
return res | CLASS_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR RETURN VAR |
Shreyansh has an integer N. He is really curious about the binary representation of integers. He sees that any given integer has a number of set bits. Now he wants to find out that how many positive integers, strictly less than N, have the same number of set bits as N.
He is a little weak in maths. Help him find the number of integers.
Note : Since N takes large values, brute force won't work.
Example 1:
Input:
N = 8
Output: 3
Explanation:
Binary representation of 8 : 1000
So the integers less than 8 with
same number of set bits are : 4, 2, 1
Hence, the result is 3.
Example 2:
Input:
N = 4
Output: 2
Explanation:
Binary representation of 4 : 100
So the integers less than 4 with
same number of set bits are : 2, 1
So, The result is 2.
Your Task:
You don't need to read input or print anything. Your task is to complete the function count() which takes the integer N as input parameters and returns the required answer.
Expected Time Complexity: O(log(n))
Expected Auxiliary Space: O(log(n)*log(n))
Constraints :
1 <= N <= 10^12 | class Solution:
def count(self, N):
def c(n, r):
if r > n:
return 0
res = 1
for i in range(r):
res *= n - i
res /= i + 1
return res
i = 0
ans = 0
cnt = 0
while N:
if N & 1 == 1:
cnt += 1
ans += c(i, cnt)
i += 1
N >>= 1
return int(ans) | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR |
Shreyansh has an integer N. He is really curious about the binary representation of integers. He sees that any given integer has a number of set bits. Now he wants to find out that how many positive integers, strictly less than N, have the same number of set bits as N.
He is a little weak in maths. Help him find the number of integers.
Note : Since N takes large values, brute force won't work.
Example 1:
Input:
N = 8
Output: 3
Explanation:
Binary representation of 8 : 1000
So the integers less than 8 with
same number of set bits are : 4, 2, 1
Hence, the result is 3.
Example 2:
Input:
N = 4
Output: 2
Explanation:
Binary representation of 4 : 100
So the integers less than 4 with
same number of set bits are : 2, 1
So, The result is 2.
Your Task:
You don't need to read input or print anything. Your task is to complete the function count() which takes the integer N as input parameters and returns the required answer.
Expected Time Complexity: O(log(n))
Expected Auxiliary Space: O(log(n)*log(n))
Constraints :
1 <= N <= 10^12 | class Solution:
def csb(self, n):
count = 0
while n > 0:
n &= n - 1
count += 1
return count
def ncr(self, n, r):
if n < r:
return 0
res = 1
for i in range(r):
res *= n - i
res //= i + 1
return res
def rec(self, N, setBits, l):
if l == 0:
return 0
mask = 1 << l
res = 0
if mask & N:
res1 = self.rec(N, setBits - 1, l - 1)
res0 = self.ncr(l, setBits)
res = res0 + res1
else:
res = self.rec(N, setBits, l - 1)
return res
def count(self, N):
setBits = self.csb(N)
return self.rec(N, setBits, 63) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR NUMBER |
Shreyansh has an integer N. He is really curious about the binary representation of integers. He sees that any given integer has a number of set bits. Now he wants to find out that how many positive integers, strictly less than N, have the same number of set bits as N.
He is a little weak in maths. Help him find the number of integers.
Note : Since N takes large values, brute force won't work.
Example 1:
Input:
N = 8
Output: 3
Explanation:
Binary representation of 8 : 1000
So the integers less than 8 with
same number of set bits are : 4, 2, 1
Hence, the result is 3.
Example 2:
Input:
N = 4
Output: 2
Explanation:
Binary representation of 4 : 100
So the integers less than 4 with
same number of set bits are : 2, 1
So, The result is 2.
Your Task:
You don't need to read input or print anything. Your task is to complete the function count() which takes the integer N as input parameters and returns the required answer.
Expected Time Complexity: O(log(n))
Expected Auxiliary Space: O(log(n)*log(n))
Constraints :
1 <= N <= 10^12 | class Solution:
def ncr(self, n, r):
if r > n:
return 0
if r == 0 or r == n:
return 1
if self.cache[n][r] != -1:
return self.cache[n][r]
res = self.ncr(n - 1, r - 1) + self.ncr(n - 1, r)
self.cache[n][r] = res
return res
def count(self, N):
self.cache = [([-1] * 40) for _ in range(40)]
res = 0
ones = 0
bits = 0
while N != 0:
if N & 1:
res += self.ncr(bits, ones + 1)
ones += 1
bits += 1
N >>= 1
return res | CLASS_DEF FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR NUMBER VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR |
Shreyansh has an integer N. He is really curious about the binary representation of integers. He sees that any given integer has a number of set bits. Now he wants to find out that how many positive integers, strictly less than N, have the same number of set bits as N.
He is a little weak in maths. Help him find the number of integers.
Note : Since N takes large values, brute force won't work.
Example 1:
Input:
N = 8
Output: 3
Explanation:
Binary representation of 8 : 1000
So the integers less than 8 with
same number of set bits are : 4, 2, 1
Hence, the result is 3.
Example 2:
Input:
N = 4
Output: 2
Explanation:
Binary representation of 4 : 100
So the integers less than 4 with
same number of set bits are : 2, 1
So, The result is 2.
Your Task:
You don't need to read input or print anything. Your task is to complete the function count() which takes the integer N as input parameters and returns the required answer.
Expected Time Complexity: O(log(n))
Expected Auxiliary Space: O(log(n)*log(n))
Constraints :
1 <= N <= 10^12 | class Solution:
def count(self, N):
dp = [([-1] * 64) for i in range(64)]
def solve(spots, ones):
if ones > spots:
return 0
if ones == 0 or ones == spots:
return 1
if dp[spots][ones] != -1:
return dp[spots][ones]
res = solve(spots - 1, ones - 1) + solve(spots - 1, ones)
dp[spots][ones] = res
return res
out = 0
ones = 0
for i in range(64):
if N & 1:
ones += 1
out += solve(i, ones)
N >>= 1
if N == 0:
break
return out | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR NUMBER VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER RETURN VAR |
Shreyansh has an integer N. He is really curious about the binary representation of integers. He sees that any given integer has a number of set bits. Now he wants to find out that how many positive integers, strictly less than N, have the same number of set bits as N.
He is a little weak in maths. Help him find the number of integers.
Note : Since N takes large values, brute force won't work.
Example 1:
Input:
N = 8
Output: 3
Explanation:
Binary representation of 8 : 1000
So the integers less than 8 with
same number of set bits are : 4, 2, 1
Hence, the result is 3.
Example 2:
Input:
N = 4
Output: 2
Explanation:
Binary representation of 4 : 100
So the integers less than 4 with
same number of set bits are : 2, 1
So, The result is 2.
Your Task:
You don't need to read input or print anything. Your task is to complete the function count() which takes the integer N as input parameters and returns the required answer.
Expected Time Complexity: O(log(n))
Expected Auxiliary Space: O(log(n)*log(n))
Constraints :
1 <= N <= 10^12 | import sys
sys.setrecursionlimit(100000)
class Solution:
def count(self, N):
def nCr(n, r):
if r > n:
return 0
ans = 1
for i in range(r):
ans *= n - i
ans /= i + 1
return int(ans)
if N == 1:
return 0
n = N
cnt = 0
i = 0
ans = 0
while n:
if n & 1 == 1:
cnt += 1
ans += nCr(i, cnt)
i += 1
n >>= 1
return ans | IMPORT EXPR FUNC_CALL VAR NUMBER CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR |
Shreyansh has an integer N. He is really curious about the binary representation of integers. He sees that any given integer has a number of set bits. Now he wants to find out that how many positive integers, strictly less than N, have the same number of set bits as N.
He is a little weak in maths. Help him find the number of integers.
Note : Since N takes large values, brute force won't work.
Example 1:
Input:
N = 8
Output: 3
Explanation:
Binary representation of 8 : 1000
So the integers less than 8 with
same number of set bits are : 4, 2, 1
Hence, the result is 3.
Example 2:
Input:
N = 4
Output: 2
Explanation:
Binary representation of 4 : 100
So the integers less than 4 with
same number of set bits are : 2, 1
So, The result is 2.
Your Task:
You don't need to read input or print anything. Your task is to complete the function count() which takes the integer N as input parameters and returns the required answer.
Expected Time Complexity: O(log(n))
Expected Auxiliary Space: O(log(n)*log(n))
Constraints :
1 <= N <= 10^12 | class Solution:
def fact(self, n):
if n == 0:
return 1
return n * self.fact(n - 1)
def count(self, N):
r = 0
i = 0
ans = 0
while N != 0:
if N & 1 == 1:
r += 1
if i == 0:
ans = 0
else:
ans += self.fact(i) // (self.fact(r) * self.fact(abs(i - r)))
i += 1
N = N >> 1
return ans | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER RETURN BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.