Section mcq’s choice Choice Choice 1,2 Choice 1,3,5


Download 63.22 Kb.
bet7/8
Sana03.06.2024
Hajmi63.22 Kb.
#1840512
1   2   3   4   5   6   7   8
Bog'liq
CODER EXAM

from enum import Enum

class Opcode(Enum):
ADD = ('ADD', 1)
SUB = ('SUB', 1)
MULT = ('MULT', 2)
DIV = ('DIV', 2)
LOAD = ('LOAD', 1)
STORE = ('STORE', 1)

# Example of using the Opcode Enum in a Node class
class Node:
def __init__(self, opcode: Opcode):
self.opcode = opcode
self.delay = opcode.value[1]

# Example usage
node = Node(Opcode.ADD)
print(f"Opcode: {node.opcode.value[0]}, Delay: {node.delay}")
These improvements aim to provide a more comprehensive and practical solution, adhering closely to the prompt's requirements and enhancing overall clarity and utility.

Writing: Q1
Prompt
Write a function shuffle(numShuffles) which first generates an ordered list of 52 playing cards, as such: [1,1,1,1,2,2,2,2,...,13,13,13,13]. Then, randomly choose two positions within the deck and swap the two cards at these two position. Do this numShuffles number of times, and then print out the resulting shuffled deck. Call this function with 10, 50, 100, and 500 shuffles.

Please respond to the above prompt


Answer
import random

def shuffle(numShuffles):
# Generate an ordered list of 52 playing cards
deck = [i for i in range(1, 14)] * 4

# Perform the specified number of shuffles
for _ in range(numShuffles):
a, b = random.sample(range(52), 2)
deck[a], deck[b] = deck[b], deck[a]

print(deck)

# Call the function with different numbers of shuffles
shuffle(10)
shuffle(50)
shuffle(100)
shuffle(500)
Please provide a 3-4 sentence explanation on your response
This code defines a ‘shuffle’ function that initially creates an ordered deck of 52 cards, represented by numbers 1 through 13 repeated four times. The function then shuffles the deck by swapping two random cards, repeating this process ‘numShuffles’ times. The function is called with 10, 50, 100, and 500 shuffles to demonstrate varying levels of randomness in the deck's order. Each shuffle increases the randomness, potentially leading to a more thoroughly mixed deck.

Writing: Q2
Write a Python function `valid steps(steps, dependencies)` that decides if there is a valid way the user could have completed exactly the steps in `steps`. A user can only complete a step if it has also completed all of that step's dependencies. Each step is an integer, `steps` will be a set of steps, and `dependencies` is the graph of dependencies between steps. For example, if `dependencies` is `{ 1: [2, 3], 2: [3], 3: [] }`, that means that 3 has no dependencies, 2 can only be completed after 3, and 1 can only be completed after 2 and 3. In that example, `{2, 3}`, `{1, 2, 3}` and `{3}` would be valid `steps` sets while `{1, 3}` is invalid (there's no way to complete only steps 1 and 3 without also completing step 2)
Code for the valid steps function that answers this question (code should be wrapped in Markdown to correctly format it, but don't put any explanation in this field, ONLY the Markdown + code + code comments). Make sure the code runs!
Answer
def valid_steps(steps, dependencies):

Download 63.22 Kb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6   7   8




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling