سوالات هفته سوم، دومین دوره لیگ مای‌کد به همراه پاسخ

سوالات هفته سوم، دومین دوره لیگ مای‌کد به همراه پاسخ

سوالات چالشی هفته دوم دومین دوره لیگ مای‌کد به همراه پاسخ‌های مورد انتظار تست کیس‌ها جهت آشنایی بیشتر شرکت‌کنندگان در لیگ منتشر می‌گردد.

پاسخ‌های تیم برنده جهت افزایش جنبه آموزشی  و استفاده سایر شرکت‌کنندگان پس از آزمون کامنت گذاری شده است.

دریافت
عنوان: نرمال سازی داده ها با min,max
حجم: 299 کیلوبایت

پاسخ تیم برنده


دریافت
عنوان: سوال چالشی الگوریتم Sleep Sort
حجم: 282 کیلوبایت

پاسخ تیم برنده 


دریافت
عنوان: سوال چالشی اعداد دوری
حجم: 607 کیلوبایت
پاسخ تیم برنده


تابع اصلی مرتبط با سوال اول

 
#Den stands for Denominator(Bottom part) and Num stands for Numerator(Upper part)

# Function to return the GCD (ک.م.م)
def gcd(a, b):
    while b > 0:
        a, b = b, a % b
    return a
# Main function to solve
def solve(user_input):
    # Getting input and splitting into a list of strings
    parts = user_input.split()
    
    # Converting strings to integers manually
    nums = []
    for item in parts:
        nums.append(int(item))
    # Nothing to return if 0
    if len(nums) == 0:
        return ""

    # Mins and Maxs (Could've used sort but oh well . . .)
    min_val = nums[0]
    max_val = nums[0]
    for n in nums:
        if n < min_val:
            min_val = n #Sets as new min
        if n > max_val:
            max_val = n #Sets as new max
    
    results = []
    
    # Special case (Rule)
    if min_val == max_val:
        for i in range(len(nums)):
            results.append("1/2")
    else:
        # Calculate the denominator 
        denominator = max_val - min_val
        #Calculate the numerator
        for x in nums:
            numerator = x - min_val
            #Special case (Rule)
            if numerator == 0:
                results.append("0")
            else:
                # Simplify the fraction using the GCD (ک.م.م)
                common_factor = gcd(numerator, denominator)
                simplified_num = numerator // common_factor
                simplified_den = denominator // common_factor
                
                # Print numerator if den == 1
                if simplified_den == 1:
                    results.append(str(simplified_num))
                else:
                    # Formatting 
                    fraction_string = str(simplified_num) + "/" + str(simplified_den)
                    results.append(fraction_string)
                    
    # Printing the list joined by spaces
    output = ""
    for i in range(len(results)):
        output += results[i]
        if i < len(results) - 1:
            output += " "
    return output

# Input and Output
input_file = open("input1.txt", "r")
output_file = open("output1.txt", "w")

for line in input_file:
    if line.strip():
        processed_line = solve(line)
        output_file.write(processed_line + "\n")

input_file.close()
output_file.close()

تابع اصلی مرتبط با سوال دوم

def solve(user_input):
    # Sppppliiiiit
    parts = user_input.split()
    # Convert to int
    all_numbers = [int(x) for x in parts]

    # The first number is k
    k = all_numbers[0]

    # The rest of the numbers are the sensor data
    sensor_data = all_numbers[1:]
    # Remove duplicates and then sort 
    unique_sorted_data = sorted(list(set(sensor_data)))
    # Check if k is larger than the number of unique items available
    
    if k > len(unique_sorted_data): # !Must be lower!
        return "UNKNOWN"
    else:
        # prints the smallest k
        return str(unique_sorted_data[k-1])

# Input and Output
input_file = open("input2.txt", "r")
output_file = open("output2.txt", "w")

for line in input_file:
    if line.strip():
        processed_line = solve(line)
        output_file.write(processed_line + "\n")

input_file.close()
output_file.close()

تابع اصلی مرتبط با سوال سوم

 
def solve_puzzle(line_text):
    # (1/17 through 16/17)
    valid_rotations = {}
    k_to_string = {}
    for k in range(1, 17):
        value = (k * (10**16)) // 17 #Set to 16 digits and divide by 17
        s_val = str(value).zfill(16) #Set to 16 digits , fill with 0s if needed
        valid_rotations[s_val] = k #For pos
        k_to_string[k] = s_val #For actual num
        
    user_input = line_text.split()
    ring_outer = user_input[0]
    ring_middle = user_input[1]
    
    if ring_outer in valid_rotations and ring_middle in valid_rotations:
        # Get the 'k' value (the numerator) for each input
        k1 = valid_rotations[ring_outer]
        k2 = valid_rotations[ring_middle]
        # Calculate the sum for the inner ring
        k3 = k1 + k2
        # If the resulting k is between 1 and 16, it's a valid cyclic rotation
        if k3 in k_to_string:
            return k_to_string[k3]
        else:
            # If the sum > 16, it's no longer a simple rotation (as in Case 3)
            return "INVALID"
    else:
        # If one of the inputs wasn't a valid rotation to begin with
        return "INVALID"

# Input and Output
input_file = open("input3.txt", "r")
output_file = open("output3.txt", "w")

for line in input_file:
    if line.strip():
        processed_line = solve_puzzle(line)
        output_file.write(processed_line + "\n")

input_file.close()
output_file.close()

۰ ۰ ۰ دیدگاه

دیدگاه‌ها

هیچ نظری هنوز ثبت نشده است.
ارسال نظر آزاد است، اما اگر قبلا در بیان ثبت نام کرده اید می توانید ابتدا وارد شوید.
شما میتوانید از این تگهای html استفاده کنید:
<b> یا <strong>، <em> یا <i>، <u>، <strike> یا <s>، <sup>، <sub>، <blockquote>، <code>، <pre>، <hr>، <br>، <p>، <a href="" title="">، <span style="">، <div align="">
تجدید کد امنیتی

لیگ دانش‌آموزی ACM - مای‌کُد

این لیگ در طول سالی تحصیلی و از آذر‌ماه تا فروردین‌ماه سال بعد به صورت ترکیبی برخط/حضوری برگزار خواهد شد.

دسته‌بندی
آخرین مطلب
پیوندها
بایگانی