Appearance
question:def caesar_cipher(s: str, k: int) -> str: Encrypts the given string `s` using the Caesar Cipher technique with shift `k`. >>> caesar_cipher("HELLO", 3) "KHOOR" >>> caesar_cipher("XYZ", 2) "ZAB" >>> caesar_cipher("HELLO", 0) "HELLO" >>> caesar_cipher("ABC", 25) "ZAB" >>> caesar_cipher("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 1) "BCDEFGHIJKLMNOPQRSTUVWXYZA" >>> caesar_cipher("A" * 100, 1) "B" * 100
answer:def caesar_cipher(s, k): Encrypts the given string `s` using the Caesar Cipher technique with shift `k`. encrypted = [] for char in s: new_char = chr(((ord(char) - ord('A') + k) % 26) + ord('A')) encrypted.append(new_char) return ''.join(encrypted)
question:from typing import List, Tuple def most_frequent_number(T: int, tablet_data: List[Tuple[int, List[int]]]) -> List[int]: Find the most frequent number on each tablet. If there are multiple numbers with the same highest frequency, return the smallest number among them. >>> most_frequent_number(2, [(5, [1, 2, 2, 3, 1]), (6, [4, 4, 4, 5, 5, 5])]) [1, 4]
answer:from collections import Counter def most_frequent_number(T, tablet_data): results = [] for i in range(T): N = tablet_data[i][0] numbers = tablet_data[i][1:] frequency = Counter(numbers) max_freq = max(frequency.values()) most_frequent = min([num for num, freq in frequency.items() if freq == max_freq]) results.append(most_frequent) return results
question:def is_playlist_valid(M: int, G: int, songs: List[int]) -> str: Determines if a playlist is valid. A playlist is valid if: 1. It has at least one song. 2. Any consecutive songs in the playlist must have different genres. >>> is_playlist_valid(5, 3, [1, 2, 1, 3, 2]) "Valid" >>> is_playlist_valid(4, 2, [1, 1, 2, 2]) "Invalid" from typing import List def test_valid_playlist(): assert is_playlist_valid(5, 3, [1, 2, 1, 3, 2]) == "Valid" def test_invalid_playlist_consecutive_genres(): assert is_playlist_valid(4, 2, [1, 1, 2, 2]) == "Invalid" def test_valid_single_song_playlist(): assert is_playlist_valid(1, 1, [1]) == "Valid" def test_invalid_empty_playlist(): assert is_playlist_valid(0, 1, []) == "Invalid" def test_valid_long_playlist(): assert is_playlist_valid(10, 3, [1, 2, 3, 1, 2, 3, 1, 2, 3, 1]) == "Valid" def test_invalid_first_two_same(): assert is_playlist_valid(3, 2, [1, 1, 2]) == "Invalid" def test_valid_max_songs(): songs = [i % 10 + 1 for i in range(100)] assert is_playlist_valid(100, 10, songs) == "Valid" def test_invalid_max_songs(): songs = [1] * 100 assert is_playlist_valid(100, 10, songs) == "Invalid"
answer:def is_playlist_valid(M, G, songs): Determines if a playlist is valid. A playlist is valid if: 1. It has at least one song. 2. Any consecutive songs in the playlist must have different genres. Parameters: - M: int, number of songs in the playlist. - G: int, number of unique genres. - songs: list of int, genres of the songs in the playlist. Returns: - "Valid" if the playlist is valid, "Invalid" otherwise. if M < 1: return "Invalid" for i in range(1, M): if songs[i] == songs[i-1]: return "Invalid" return "Valid" # Example Usage M = 5 G = 3 songs = [1, 2, 1, 3, 2] print(is_playlist_valid(M, G, songs)) # Expected Output: "Valid" M = 4 G = 2 songs = [1, 1, 2, 2] print(is_playlist_valid(M, G, songs)) # Expected Output: "Invalid"
question:from typing import Optional def nth_prime(n: int) -> Optional[int]: Write a program that, given an integer N, determines the N-th prime number. Args: n (int): a positive integer N, where 1 ≤ N ≤ 10000. Returns: Optional[int]: the N-th prime number or None if N is less than 1. Examples: >>> nth_prime(1) 2 >>> nth_prime(5) 11 >>> nth_prime(10) 29 >>> nth_prime(20) 71 >>> nth_prime(100) 541 >>> nth_prime(10000) 104729 >>> nth_prime(0) None >>> nth_prime(-5) None
answer:def nth_prime(n): Returns the N-th prime number. if n < 1: return None primes = [2] num = 3 while len(primes) < n: is_prime = True for prime in primes: if prime * prime > num: break if num % prime == 0: is_prime = False break if is_prime: primes.append(num) num += 2 return primes[n-1]