xxxxxxxxxx
from collections import deque
def BFS(a, b, target):
# Map is used to store the states, every
# state is hashed to binary value to
# indicate either that state is visited
# before or not
m = {}
isSolvable = False
path = []
# Queue to maintain states
q = deque()
# Initialing with initial state
q.append((0, 0))
while (len(q) > 0):
# Current state
u = q.popleft()
#q.pop() #pop off used state
# If this state is already visited
if ((u[0], u[1]) in m):
continue
# Doesn't met jug constraints
if ((u[0] > a or u[1] > b or
u[0] < 0 or u[1] < 0)):
continue
# Filling the vector for constructing
# the solution path
path.append([u[0], u[1]])
# Marking current state as visited
m[(u[0], u[1])] = 1
# If we reach solution state, put ans=1
if (u[0] == target or u[1] == target):
isSolvable = True
if (u[0] == target):
if (u[1] != 0):
# Fill final state
path.append([u[0], 0])
else:
if (u[0] != 0):
# Fill final state
path.append([0, u[1]])
# Print the solution path
sz = len(path)
for i in range(sz):
print("(", path[i][0], ",",
path[i][1], ")")
break
# If we have not reached final state
# then, start developing intermediate
# states to reach solution state
q.append([u[0], b]) # Fill Jug2
q.append([a, u[1]]) # Fill Jug1
for ap in range(max(a, b) + 1):
# Pour amount ap from Jug2 to Jug1
c = u[0] + ap
d = u[1] - ap
# Check if this state is possible or not
if (c == a or (d == 0 and d >= 0)):
q.append([c, d])
# Pour amount ap from Jug 1 to Jug2
c = u[0] - ap
d = u[1] + ap
# Check if this state is possible or not
if ((c == 0 and c >= 0) or d == b):
q.append([c, d])
# Empty Jug2
q.append([a, 0])
# Empty Jug1
q.append([0, b])
# No, solution exists if ans=0
if (not isSolvable):
print ("No solution")
# Driver code
if __name__ == '__main__':
Jug1, Jug2, target = 4, 3, 2
print("Path from initial state "
"to solution state ::")
BFS(Jug1, Jug2, target)
# This code is contributed by mohit kumar 29
xxxxxxxxxx
#include<stdio.h>
using namespace std;
void main()
{
for (int i=1, i<=5, i++)
{
for(int j=1, j<=i,j++)
{
printf("%c",64+j)
}
printf("\n")
}
}
xxxxxxxxxx
import math
x,y=input().split(),input().split()
x,y=[int(i) for i in x],[int(i) for i in y]
t,t1=abs(x[0]-y[0]),abs(x[1]-y[1])
print("%.2f" %math.sqrt(t*t+t1*t1))
xxxxxxxxxx
a=[]
def knapsack(pro,wt,c,n,ans):
global a
if n==0 or c==0:
a+=ans,
elif wt[n-1]>c:
knapsack(pro,wt,c,n-1,ans)
else:
knapsack(pro,wt,c-wt[n-1],n-1,ans+pro[n-1])
knapsack(pro,wt,c,n-1,ans)
n=int(input())
profit=list(map(int,input().split()))
weights=list(map(int,input().split()))
capacity=int(input())
knapsack(profit,weights,capacity,n,0)
a.sort(reverse=True)
print(a[1 if a[0]<=10 and a[0]%3 else 0])
xxxxxxxxxx
n=int(input())
for i in range (n):
a,b,k=(map(int,input().split()))
if a>=b:
print(k//b)
else:
print(k//a)
xxxxxxxxxx
import random
from collections import defaultdict
def main_roll():
dice_amount = int(input("Enter the number of dice: ")) # Total Number of Dice Being Rolled
sides_of_dice = int(input("Enter the number of sides: ")) # Total Number of Sides per Die
rolls_of_dice = int(input("Enter the number of rolls to simulate: ")) # Total Number of Times Each Die Rolled
result = roll(dice_amount, sides_of_dice, rolls_of_dice) # This stores the results
maxH = 0 # Used for formulating
for i in range(dice_amount, dice_amount * sides_of_dice + 1):
if result[i] / rolls_of_dice > maxH: maxH = result[i] / rolls_of_dice
for i in range(dice_amount, dice_amount * sides_of_dice + 1):
print('{:2d}{:10d}{:8.2%} {}'.format(i, result[i], result[i] / rolls_of_dice, '#' * int(result[i] / rolls_of_dice / maxH * 40)))
def roll(dice_amount, sides_of_dice, rolls):
d = defaultdict(int)
for _ in range(rolls):
d[sum(random.randint(1, sides_of_dice) for _ in range(dice_amount))] += 1
return d
main_roll()
xxxxxxxxxx
#include<iostream>
using namespace std;
#include<bits/stdc++.h>
int has1[26],has2[26];
int main()
{
int n;
cin>>n;
char ff[n+10],ss[n+10];
for(int i=0;i<n/2;i++)
{
cin>>ff[i];
has1[ff[i]-'a']++;
}
for(int i=0;i<n/2;i++)
{
cin>>ss[i];
has2[ss[i]-'a']++;
}
sort(ff,ff+n/2);
sort(ss,ss+n/2);
//case 1
int ans1=0,ans2=0,ans3=0;
for(int i=0;i<26;i++)
{
ans1+=abs(has1[i]-has2[i]);
// cout<<"i " <<i<<" " <<has1[i]<<" " <<has2[i]<<endl;
}
int i=0,j=0;
while(i!=n/2 && j!=n/2)
{
if(ff[i]<ss[j])
{
i++;
j++;
}
else
j++;
}
ans2=abs(j-i);
i=0,j=0;
while(i!=n/2 && j!=n/2)
{
if(ss[i]<ff[j])
{
i++;
j++;
}
else
j++;
}
ans3=abs(j-i);
// cout<<ans1<<" "<<ans2<<" "<<ans3<<endl;
cout<<min(min(ans1/2,ans2),ans3);
return 0;
}
xxxxxxxxxx
def djikstra(graph, initial):
visited_weight_map = {initial: 0}
nodes = set(graph.nodes)
# Haven't visited every node
while nodes:
next_node = min(
node for node in nodes if node in visited
)
if next_node is None:
# If we've gone through them all
break
nodes.remove(next_node)
current_weight = visited_weight_map[next_node]
for edge in graph.edges[next_node]:
# Go over every edge connected to the node
weight = current_weight + graph.distances[(next_node, edge)]
if edge not in visited_weight_map or weight < visited_weight_map[edge]:
visited_weight_map[edge] = weight
return visited
xxxxxxxxxx
# cook your dish here
tc = int(input())
for _ in range(tc):
c = 0
n = int(input())
n = pow(2,n)
for x in range(0,n):
a = x ^ (x+1)
b = (x+2) ^ (x+3)
if(a==b):
c+=1
print(c)