ট্র্যাক
/
Python
Python
/
অনুশীলনী
/
গ্রেড নির্ধারণ
গ্রেড নির্ধারণ

গ্রেড নির্ধারণ

লার্নিং অনুশীলনী

ভূমিকা

Python-এ লুপ করার দুইটি কনস্ট্রাক্ট রয়েছে। while লুপ অনির্দিষ্ট (গণনাহীন) ইটারেশনের জন্য, আর for লুপ নির্দিষ্ট (গণনাযুক্ত) ইটারেশনের জন্য। break, continue ও else কিওয়ার্ডগুলো লুপের আচরণ কাস্টমাইজ করতে সাহায্য করে। range() ও enumerate() লুপ গণনা এবং ইনডেক্সিংয়ে সাহায্য করে।

While

while লুপ ততক্ষণ চলতেই থাকবে, যতক্ষণ loop expression বা "test" একটি boolean context-এ True হিসেবে গণ্য হয়; আর সেটি False হিসেবে গণ্য হলে থেমে যায়:


# Lists are considered "truthy" in a boolean context if they
# contain one or more values, and "falsy" if they are empty.

>>> placeholders = ["spam", "ham", "eggs", "green_spam", "green_ham", "green_eggs"]

>>> while placeholders:
...     print(placeholders.pop(0))
...
'spam'
'ham'
'eggs'
'green_spam'
'green_ham'
'green_eggs'

For

Python-এ সাধারণ for loop-কে বরং একটি for each বলা ভালো, যা যেকোনো ইটারেবল অবজেক্ট-এর মানগুলোর মধ্য দিয়ে ঘোরে, এবং next() কল করে আর কোনো মান ফেরত না এলে থেমে যায়:


>>> word_list = ["bird", "chicken", "barrel", "bongo"]

>>> for word in word_list:
...    if word.startswith("b"):
...        print(f"{word.title()} starts with a B.")
...    else:
...        print(f"{word.title()} doesn't start with a B.")
...
'Bird starts with a B.'
'Chicken doesn\'t start with a B.'
'Barrel starts with a B.'
'Bongo starts with a B.'

সিকোয়েন্স অবজেক্ট range()

যখন নির্দিষ্ট কোনো iterable দেওয়া থাকে না, তখন লুপ কাউন্টার হিসেবে বিশেষ range() সিকোয়েন্সটি ব্যবহার করা হয়। range()-এর সিকোয়েন্সটি কোথায় stop হবে তা নির্দেশ করার জন্য একটি int দরকার, আর এটি ঐচ্ছিকভাবে start ও step প্যারামিটারও নিতে পারে। কোনো start সংখ্যা না দিলে, সিকোয়েন্সটি ০ দিয়ে শুরু হবে। range() অবজেক্টগুলো লেজি (চাহিদা অনুযায়ী মান তৈরি হয়), সব সাধারণ সিকোয়েন্স অপারেশন সমর্থন করে, এবং নির্দিষ্ট সিকোয়েন্স যত লম্বাই হোক না কেন, নির্দিষ্ট পরিমাণ মেমোরি নেয়।

# Here we use range to produce some numbers, rather than creating a list of them in memory.
# The values will start with 1 and stop *before* 7

>>> for number in range(1, 7):
...    if number % 2 == 0:
...       print(f"{number} is even.")
...    else:
...       print(f"{number} is odd.")
'1 is odd.'
'2 is even.'
'3 is odd.'
'4 is even.'
'5 is odd.'
'6 is even.'

# range() can also take a *step* parameter.
# Here we use range to produce only the "odd" numbers, starting with 3 and stopping *before* 15.

>>> for number in range(3, 15, 2):
...    if number % 2 == 0:
...       print(f"{number} is even.")
...    else:
...       print(f"{number} is odd.")
...
'3 is odd.'
'5 is odd.'
'7 is odd.'
'9 is odd.'
'11 is odd.'
'13 is odd.'

enumerate() দিয়ে মান ও ইনডেক্স

যদি মান এবং তাদের ইনডেক্স দুটোই দরকার হয়, তাহলে বিল্ট-ইন enumerate(<iterable>) (index, value) জোড়া রিটার্ন করবে:


>>> word_list = ["bird", "chicken", "barrel", "apple"]

# *index* and *word* are the loop variables.
# Loop variables can be any valid python name.

>>> for index, word in enumerate(word_list):
...    if word.startswith("b"):
...        print(f"{word.title()} (at index {index}) starts with a B.")
...    else:
...        print(f"{word.title()} (at index {index}) doesn't start with a B.")
...
'Bird (at index 0) starts with a B.'
'Chicken (at index 1) doesn\'t start with a B.'
'Barrel (at index 2) starts with a B.'
'Apple (at index 3) doesn\'t start with a B.'


# The same method can be used as a "lookup" for pairing items between two lists.
# Of course, if the lengths or indexes don't line up, this doesn't work.

>>> word_list = ["cat", "chicken", "barrel", "apple", "spinach"]
>>> category_list = ["mammal", "bird", "thing", "fruit", "vegetable"]

>>> for index, word in enumerate(word_list):
...    print(f"{word.title()} is in category: {category_list[index]}.")
...
'Cat is in category: mammal.'
'Chicken is in category: bird.'
'Barrel is in category: thing.'
'Apple is in category: fruit.'
'Spinach is in category: vegetable.'

লুপের আচরণ পরিবর্তন

continue কিওয়ার্ডটি দিয়ে পরের ইটারেশন সাইকেলে এগিয়ে যাওয়া যায়:

word_list = ["bird", "chicken", "barrel", "bongo", "sliver", "apple", "bear"]

# This will skip *bird*, at index 0
for index, word in enumerate(word_list):
    if index == 0:
        continue
    if word.startswith("b"):
        print(f"{word.title()} (at index {index}) starts with a b.")

'Barrel (at index 2) starts with a b.'
'Bongo (at index 3) starts with a b.'
'Bear (at index 6) starts with a b.'

break (অনেক C-সংক্রান্ত ভাষার মতো) কিওয়ার্ডটি দিয়ে ইটারেশন থামানো যায় এবং সবচেয়ে ভেতরের loop থেকে "বেরিয়ে যাওয়া" যায়:

>>>  word_list = ["bird", "chicken", "barrel", "bongo", "sliver", "apple"]

>>> for index, word in enumerate(word_list):
...    if word.startswith("b"):
...        print(f"{word.title()} (at index {index}) starts with a B.")
...    elif word == "sliver":
...       break
...    else:
...       print(f"{word.title()} doesn't start with a B.")
... print("loop broken.")
...
'Bird (at index 0) starts with a B.'
'Chicken doesn\'t start with a B.'
'Barrel (at index 2) starts with a B.'
'Bongo (at index 3) starts with a B.'
'loop broken.'

নির্দেশনা

আপনি একজন শিক্ষক সহকারী, শিক্ষার্থীদের পরীক্ষার খাতা মূল্যায়ন করছেন। হাতে-কলমে ফলাফলের হিসাব রাখা 점점 ক্লান্তিকর আর ভুলপ্রবণ হয়ে উঠছে। আপনি ঠিক করলেন, ক্লাসের ফলাফল গুনতে ও হিসাব করতে কিছু ফাংশন বানিয়ে বিষয়টা আরেকটুinteresting করে তুলবেন।

1. স্কোর রাউন্ডিং

পরীক্ষার প্রশ্নে আপনি "আংশিক নম্বর" দিতে পারলেও, সামগ্রিক পরীক্ষার স্কোর অবশ্যই int হতে হবে। তাই ক্লাসের স্কোর নিয়ে অন্য কিছু করার আগে, আপনাকে সব গ্রেড একবার ঘুরে দেখে যেকোনো float স্কোরকে int-এ রূপান্তর করতে হবে। ভাগ্য ভালো, Python-এ ব্যবহারের জন্য round() নামের একটি বিল্ট-ইন ফাংশন আছে।

round_scores(student_scores) নামের ফাংশনটি তৈরি করুন, যা student_scores-এর একটি list নেয়। এই ফাংশনটি ইনপুট list-টি কনজিউম করবে এবং সব স্কোর int-এ রূপান্তরিত একটি নতুন অ্যারে return করবে। ফলাফল list-এ স্কোরগুলোর ক্রম গুরুত্বপূর্ণ নয়।

>>> student_scores = [90.33, 40.5, 55.44, 70.05, 30.55, 25.45, 80.45, 95.3, 38.7, 40.3]
>>> round_scores(student_scores)
...
[40, 39, 95, 80, 25, 31, 70, 55, 40, 90]

2. পাস না করা শিক্ষার্থী

পরীক্ষার খাতা মূল্যায়নের সময় আপনি লক্ষ্য করলেন, কিছু শিক্ষার্থী আপনার প্রত্যাশামতো ভালো করছে না। কিন্তু আপনি মনোযোগ হারিয়ে ফেলেছিলেন, আর ঠিক কতজন শিক্ষার্থী ছিল, তা লিখে রাখতে ভুলে গেছেন।

count_failed_students(student_scores) নামের ফাংশনটি তৈরি করুন, যা student_scores-এর একটি list নেয়। এই ফাংশনটি পাস করার স্কোর নেই এমন শিক্ষার্থীর সংখ্যা গুনবে এবং সেই সংখ্যাটি একটি ইন্টিজার হিসেবে রিটার্ন করবে। পরীক্ষায় পাস করার জন্য একজন শিক্ষার্থীর স্কোর ৪০-এর বেশি হতে হবে।

>>> count_failed_students(student_scores=[90,40,55,70,30,25,80,95,38,40])
5

3. "সেরা"

আপনি যাকে সহায়তা করছেন সেই শিক্ষক পরীক্ষায় "সেরা" করা শিক্ষার্থীদের দল খুঁজে বের করতে চান। "সেরা" বলতে কী বোঝায় তা বদলে যায়, তাই আপনাকে বর্তমান থ্রেশহোল্ডের সমান বা তার বেশি এমন শিক্ষার্থীদের স্কোর খুঁজে বের করতে হবে।

above_threshold(student_scores, threshold) ফাংশনটি তৈরি করুন, যেটি প্যারামিটার হিসেবে student_scores (গ্রেডের একটি list) এবং threshold ("টপ স্কোর" থ্রেশহোল্ড) নেয়। এই ফাংশনটি threshold-এর >= এমন সব স্কোরের একটি list রিটার্ন করবে।

>>> above_threshold(student_scores=[90,40,55,70,30,68,70,75,83,96], threshold=75)
[90,75,83,96]

4. লেটার গ্রেড গণনা

আপনি যাকে সহায়তা করছেন সেই শিক্ষক সংখ্যাসূচক স্কোরের পাশাপাশি লেটার গ্রেডও দিতে পছন্দ করেন। শিক্ষার্থীরা পরীক্ষায় খুব কমই ১০০ পায়, তাই "লেটার গ্রেড"-এর নিম্ন থ্রেশহোল্ড সর্বোচ্চ অর্জিত স্কোরের ভিত্তিতে হিসাব করা হয় এবং সর্বোচ্চ স্কোর থেকে <= ৪০ ফেলের থ্রেশহোল্ডের মধ্যে সমানভাবে বাড়ানো হয়।

letter_grades(highest) ফাংশনটি তৈরি করুন, যেটি পরীক্ষার "সর্বোচ্চ" স্কোর আর্গুমেন্ট হিসেবে নেয় এবং প্রতিটি "আমেরিকান স্টাইল" গ্রেড ব্যবধানের জন্য নিম্ন স্কোর থ্রেশহোল্ডের একটি list রিটার্ন করে: ["D", "C", "B", "A"]।

"""Where the highest score is 100, and failing is <= 40.
       "F" <= 40
 41 <= "D" <= 55
 56 <= "C" <= 70
 71 <= "B" <= 85
 86 <= "A" <= 100
"""

>>> letter_grades(highest=100)
[41, 56, 71, 86]


"""Where the highest score is 88, and failing is <= 40.
       "F" <= 40
 41 <= "D" <= 52
 53 <= "C" <= 64
 65 <= "B" <= 76
 77 <= "A" <= 88
"""

>>> letter_grades(highest=88)
[41, 53, 65, 77]

5. নামের সাথে স্কোর মেলানো

আপনার কাছে পরীক্ষার স্কোরের একটি অ্যারে আছে, যা অবরোহ ক্রমে সাজানো, আর শিক্ষার্থীদের নামের আরেকটি অ্যারে-ও আছে, যেটিও তাদের পরীক্ষার স্কোর অনুসারে অবরোহ ক্রমে সাজানো। আপনি প্রতিটি শিক্ষার্থীর নামের সাথে তার পরীক্ষার স্কোর মিলিয়ে সামগ্রিক ক্লাস র‍্যাংকিং প্রিন্ট করতে চান।

student_ranking(student_scores, student_names) ফাংশনটি তৈরি করুন, যার প্যারামিটার student_scores এবং student_names। student_names list-এর প্রতিটি শিক্ষার্থীর নামের সাথে student_scores list থেকে তার স্কোর মিলান। আপনি ধরে নিতে পারেন যে প্রতিটি আর্গুমেন্ট list সর্বোচ্চ স্কোরধারী থেকে সর্বনিম্ন স্কোরধারী পর্যন্ত সাজানো থাকবে। ফাংশনটি <rank>. <student name>: <student score> ফরম্যাটে স্ট্রিংয়ের একটি list রিটার্ন করবে।

>>> student_scores = [100, 99, 90, 84, 66, 53, 47]
>>> student_names =  ['Joci', 'Sara','Kora','Jan','John','Bern', 'Fred']
>>> student_ranking(student_scores, student_names)
...
['1. Joci: 100', '2. Sara: 99', '3. Kora: 90', '4. Jan: 84', '5. John: 66', '6. Bern: 53', '7. Fred: 47']

6. একটি "পারফেক্ট" স্কোর

যদিও পরীক্ষায় ১০০-এর একটি "পারফেক্ট" স্কোর বিরল, তবুও অন্তত একজন শিক্ষার্থী তা অর্জন করেছে কি না জানতে ভালো লাগে।

perfect_score(student_info) ফাংশনটি তৈরি করুন, যার প্যারামিটার student_info। student_info হলো এমন একটি list, যার প্রতিটি এলিমেন্ট আরেকটি অ্যারে; প্রতিটিতে একজন শিক্ষার্থীর নাম ও স্কোর থাকে: [["Charles", 90], ["Tony", 80]]। ফাংশনটি পরীক্ষায় ১০০ পাওয়া শিক্ষার্থীর প্রথম [<name>, <score>] জোড়াটি return করবে।

যদি student_info-তে কোনো ১০০ স্কোর না পাওয়া যায়, তাহলে একটি খালি অ্যারে [] রিটার্ন করতে হবে।

>>> perfect_score(student_info=[["Charles", 90], ["Tony", 80], ["Alex", 100]])
["Alex", 100]

>>> perfect_score(student_info=[["Charles", 90], ["Tony", 80]])
[]
GitHub-এর মাধ্যমে সম্পাদনা করুন লিংকটি একটি নতুন উইন্ডো বা ট্যাবে খোলে
Python Exercism

গ্রেড নির্ধারণ শুরু করতে প্রস্তুত?

Exercism-এ সাইন আপ করুন, Python ট্র্যাকের 17টি কনসেপ্ট146টি অনুশীলনী আর সত্যিকারের মানুষের মেন্টরিং দিয়ে শিখুন ও দক্ষ হয়ে উঠুন, সম্পূর্ণ বিনামূল্যে।