Python Code Practice

savitry.in
0
Python समस्याएं और समाधान

Python Problem Statement And Solution

Problem Statement 1: बाइनरी सर्च

PS: एक सॉर्टेड लिस्ट में किसी विशेष डेटा को खोजें।

Solution: बाइनरी सर्च एल्गोरिदम का उपयोग करें।

        
        
def binary_search(sorted_list, target):
    left, right = 0, len(sorted_list) - 1
    while left <= right:
        mid = (left + right) // 2
        if sorted_list[mid] == target:
            return mid
        elif sorted_list[mid] < target:
            left = mid + 1
        else:
            right = mid - 1
    return -1
        
        
        

Example usage:
data = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
target = 11 # Data to search for

Explaination: लिस्ट को बार-बार आधे-आधे हिस्से में विभाजित करके सर्च करें।

समस्या 2: दिन या रात

समस्या: वर्तमान समय के आधार पर दिन या रात का पता लगाएं।

समाधान: `time` मॉड्यूल का उपयोग करें।


import time

def is_dark_outside():
    current_time = time.localtime()
    hour = current_time.tm_hour
    sunrise, sunset = 6, 18
    if sunrise <= hour < sunset:
        return "It is day outside."
    else:
        return "It is night outside."
        

व्याख्या: वर्तमान समय को सनराइज और सनसेट समय से तुलना करें।

समस्या 3: दो स्थानों के बीच की दूरी

समस्या: दो स्थानों के बीच की दूरी कैलकुलेट करें।

समाधान: हैवर्साइन फॉर्मूला का उपयोग करें।


import math

def haversine(lat1, lon1, lat2, lon2):
    R = 6371.0
    lat1, lon1, lat2, lon2 = map(math.radians, [lat1, lon1, lat2, lon2])
    dlat, dlon = lat2 - lat1, lon2 - lon1
    a = math.sin(dlat / 2)**2 + math.cos(lat1) * math.cos(lat2) * math.sin(dlon / 2)**2
    c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
    return R * c
        

व्याख्या: अक्षांश और देशांतर का उपयोग करके दूरी कैलकुलेट करें।

समस्या 4: बैंक सिस्टम

समस्या: नकद निकासी, नकद जमा, और पासवर्ड बदलें।

समाधान: `if-else` और फ़ंक्शन्स का उपयोग करें।


balance = 10000
password = "bank123"

def withdraw(amount):
    global balance
    if amount > balance:
        print("Insufficient balance!")
    else:
        balance -= amount
        print(f"Withdrawal successful! Updated balance: {balance}")

def deposit(amount):
    global balance
    balance += amount
    print(f"Deposit successful! Updated balance: {balance}")

def change_password():
    global password
    current_password = input("Enter your current password: ")
    if current_password == password:
        new_password = input("Enter your new password: ")
        password = new_password
        print("Password changed successfully!")
    else:
        print("Incorrect current password!")
        

व्याख्या: यूजर के इनपुट के आधार पर संबंधित ऑपरेशन करें।

समस्या 5: रिकर्सन का उपयोग करके फैक्टोरियल

समस्या: किसी संख्या का फैक्टोरियल कैलकुलेट करें।

समाधान: रिकर्सन का उपयोग करें।


def factorial(n):
    if n == 0 or n == 1:
        return 1
    else:
        return n * factorial(n - 1)
        

व्याख्या: \( n! = n \times (n-1)! \) के सिद्धांत का उपयोग करें।

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)
Our website uses cookies to enhance your experience. Learn More
Accept !