Nexclap

Kitty's Calculations on a Tree

The first line contains an integer,k , the size of the set. The second line contains k space-separated integers, the set's elements.

# Enter your code here. Read input from STDIN. Print output to STDOUT from itertools import combinations from queue import Queue from collections import defaultdict as dd,deque def find_shortest_path(graph, start, end): dist = {start: [start]} que = deque() que.append(start) while len(que): at = que.popleft() for next in graph[at]: if next not in dist: dist[next] = dist[at]+[next] que.append(next) return dist.get(end) def add(d,a,b): d[a].append(b) d[b].append(a) if __name__=='__main__': d=dd(list) t,q=map(int,input().split()) for _ in range(t-1): a,b=map(int,input().split()) add(d,a,b) for _ in range(q): k=int(input()) l=list(map(int,input().split())) if len(l)>1: s=list(combinations(l,2)) sum=0 for j in s: u,v=j p=find_shortest_path(d,u,v) dist=len(p)-1 sum=sum+(u*v*dist) result=sum%((10**9)+7) print(result) else: print(0) continue

drive function in car class

The user creates their own car and the drive function drives the car and displays statistics on how the drive went


import random class Car: def __init__(self, name, capacity, mpg, cost): #the name of the car self.name = name #the car's odometer self.miles = 0.0 #the car's current fuel self.fuel = float(capacity) #the car's maximum fuel self.capacity = float(capacity) #the car's fuel efficiency self.mpg = float(mpg) #the car's tire health self.tiretreads = 5000.0 #the car's body damage self.damage = 0 #total costs spent on the car self.costs = float(cost) #def repair, def drive,def refuel,def tire rotation,def info def drive(self,distance): if self.fuel*self.mpg < distance: print(" you need to refuel ") elif self.tiretreads < distance: print(" you have to change tires") elif self.damage > 50: print(" your car is to damaged to drive") else: crashchance = distance/1000 if round(random.uniform(0,100),2) <= crashchance: newdistance = random.randint(1,distance) print("your car crashed but it drove " + str(newdistance) + " miles") self.damage+=random.randint(1,100) print("your car was damaged " + str(self.damage)) self.miles+=newdistance self.tiretreads-=newdistance self.fuel-=newdistance/self.mpg print("you drove " + str(newdistance) + " miles") print("you now have " + str(self.fuel) + " gallons of fuel") else: self.miles+=distance self.tiretreads-=distance self.fuel-=distance/self.mpg print("=====================") print("you drove " + str(distance) + " miles") print("you now have " + str(self.fuel) + " gallons of fuel") print("=====================") for i in range(200): car1= Car("Tesla",50,25,50000) car1.drive(600)

Guessing Game homework

This is my guessing game homework

Announcements
  • Test Announcement from Nexclap

    Test message from nexclap - please ignore

    2023-02-13

  • Test message

    Test message from nexclap - please ignore

    2023-02-11

  • Test message

    Test message from nexclap - please ignore

    2023-02-11

  • View all