0% found this document useful (0 votes)
10 views7 pages

Python Code for Triangle and Names

The document describes a problem to calculate the angle MBC of a right triangle given the lengths of sides AB and BC. It provides sample input/output and code to calculate the angle in radians, convert it to degrees and round to the nearest integer. The code uses math functions like atan, degrees and round.

Uploaded by

supreet
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views7 pages

Python Code for Triangle and Names

The document describes a problem to calculate the angle MBC of a right triangle given the lengths of sides AB and BC. It provides sample input/output and code to calculate the angle in radians, convert it to degrees and round to the nearest integer. The code uses math functions like atan, degrees and round.

Uploaded by

supreet
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Medium 2:

Given a triangle ABC which is right angled at B, and M is the midpoint of AC. Now, join B and M and find
the angle MBC with AB and BC given as inputs

Example:

Input Format

The first line contains the length of side AB.

The second line contains the length of side BC.

Output Format

Output

in degrees.

Note: Round the angle to the nearest integer.

Examples:

If angle is 56.5000001°, then output 57°.

If angle is 56.5000000°, then output 57°.

If angle is 56.4999999°, then output 56°.


Sample Input

10

10

Sample Output

45°

Solution:

import math

# Input

AB = float(input())

BC = float(input())

# Calculate the angle MBC in radians using the arctan function

angle_MBC = [Link](AB / BC)

# Convert the angle from radians to degrees and round it to the nearest integer

angle_MBC_degrees = round([Link](angle_MBC))

# Output the result

print(str(angle_MBC_degrees) + '°')
Easy 1:

Given first name and last name, you need to capitalize the first letter of the first name and last name
and insert it into the sentence "My name is First name Last name.".

Input Format

A single line of input containing the full name.

Constraints

The string consists of alphanumeric characters and spaces.

Note: in a word only the first character is capitalized. Example 12abc when capitalized remains 12abc.

Output Format

Print the capitalized string in the sentence.

Sample Input

chris alan

Sample Output

My name is Chris Alan.


Solution:

# Input

full_name = input()

# Split the full name into first name and last name

first_name, last_name = full_name.split()

# Capitalize the first letter of the first name and last name

first_name = first_name.capitalize()

last_name = last_name.capitalize()

# Create the output sentence

output_sentence = f"My name is {first_name} {last_name}."

# Output the result

print(output_sentence)

Easy 2:

Given Marks of the students and a name as [Link] the average of the marks array for the student
name provided, showing 2 places after the decimal.

Input Format

The first line contains the integer, the number of students' records. The next lines contain the names
and marks obtained by a student, each value separated by a space. The final line contains query_name,
the name of a student to query.
Output Format

Print one line: The average of the marks obtained by the particular student correct to 2 decimal places.

Sample Input 0

Krishna 67 68 69

Arjun 70 98 63

Malika 52 56 60

Malika

Sample Output 0

56.00

Solution:

# Input the number of students' records

n = int(input())

# Create a dictionary to store student names and their respective marks

student_marks = {}

# Input the student names and marks and store them in the dictionary

for _ in range(n):

line = input().split()
name = line[0]

marks = list(map(float, line[1:]))

student_marks[name] = marks

# Input the student name to query

query_name = input()

# Calculate the average marks for the specified student

average_marks = sum(student_marks[query_name]) / len(student_marks[query_name])

# Print the result with 2 decimal places

print("{:.2f}".format(average_marks))

Given string will contain a first name, last name, and an id. Spaces in the string can be separated by any
number of zeros. And the Id will not contain any zero. Print the first name, last name and Id in a
dictionary.

Input Format

The first line contains the string.

Output Format

Print the dictionary {"first_name": "", "last_name": "", "id": ""}

Sample Input
Robert000Smith000123

Sample Output 0

{"first_name": "Robert", "last_name": "Smith", "id": "123"}

Solution:

import re

# Input the string

input_string = input()

# Use regular expressions to extract the first name, last name, and ID

match = [Link](r"(\D+)0*(\D+)0*(\d+)", input_string)

# Create a dictionary with the extracted values

result = {

"first_name": [Link](1),

"last_name": [Link](2),

"id": [Link](3)

# Print the dictionary

print(result)

Common questions

Powered by AI

The average of a student's marks is computed by first reading the student names and marks into a dictionary. To get the average for a queried student, retrieve the list of marks from the dictionary for the given student name and calculate the sum of these marks divided by the number of marks. The result is formatted to two decimal places .

The tangent function relates an angle in a right triangle to the ratio of the lengths of the opposite side to the adjacent side. For angle MBC, the tangent is the ratio AB/BC, with AB opposite and BC adjacent. This principle ensures the angle's accuracy since the properties of the tangent function are consistent and precise for right-angled triangles, allowing for reliable determination through inverse tangent operations .

To calculate a student's average marks, first store each student’s marks in a dictionary keyed by their name. Read inputs for each student, split them into name and marks, and convert the marks to floats. After populating the dictionary, retrieve the list of marks for the queried student, compute the average using sum and division, and print the average formatted to two decimal places .

Regular expressions are utilized to parse a string containing a first name, last name, and ID where components are separated by zeros. The regular expression pattern '(\D+)0*(\D+)0*(\d+)' captures the non-digit characters before and after zeros as the first and last names, and digits at the end as the ID. This method facilitates efficient extraction and dictionary population .

When capitalizing names with specific input constraints that include alphanumeric characters, it's crucial to correctly handle the capitalization of only the first letter of each word while maintaining the case of numerical prefixes or any existing capital letters. This ensures that names such as '12abc' are left unchanged except for ensuring each character is properly formatted in the context .

To capitalize and format a full name correctly, split the input string into the first and last name components. Capitalize the first letter of each component and insert them into the template sentence 'My name is First name Last name.'. This involves basic string operations, ensuring that only the first letter is capitalized, with any leading numbers preserved .

Rounding is necessary when converting an angle from radians to degrees to ensure the angle is expressed as a whole number degree. The angle is rounded to the nearest integer, which involves rounding half up (e.g., 56.5000001° becomes 57° while 56.4999999° becomes 56°) to comply with standard mathematical rounding rules .

The angle MBC in a right triangle where ABC is right-angled at B can be calculated using the tangent function. The formula used is angle_MBC = atan(AB/BC), where AB and BC are the lengths of the sides opposite and adjacent to the angle MBC, respectively. The angle in radians is then converted to degrees and rounded to the nearest integer .

When handling formatted input strings with variable separator lengths, regular expressions can be effectively used to dynamically identify and split different components. By matching patterns of interest (like names and IDs) surrounded by separators (like zeros), it becomes possible to flexibly manipulate and retrieve structured data regardless of separator variation .

Verification of a regular expression pattern involves testing it with various input cases that contain the intended components in different formats. This includes checking for edge cases such as leading/trailing zeros or missing components. Successful extraction and correct matches against expected outputs affirm the pattern’s reliability. Additionally, tool-assisted regex testers can help visualize matches and troubleshoot regex complexity .

You might also like