Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Generating Repeated Digits, Base Conversion, and Character Counting in Python

Tech May 14 1

Problem 1: Generating a Number with B Repetitions of Digit A

Score: 15

Read two positive integers A (1 ≤ A ≤ 9) and B (1 ≤ B ≤ 10). Generate the integer consisting of B repetitions of the digit A.

Input Format:

A single line containing A and B separatde by a comma and optional spaces.

Output Format:

A single line containing the resulting integer.

Sample Input 1:

  1,  5

Sample Output 1:

11111

Sample Input 2:

        3  ,4

Sample Output 2:

3333

Solution:

a, b = input().split(',')
print(a.strip() * int(b.strip()))

Problem 2: Base Conversion Using Built-in Functions

Score: 15

Enput an integer and a base, then output the decimal (base-10) representation.

Input Format:

A single line containing the integer value and the base, separated by a comma.

Output Format:

A single line with the decimal result.

Sample Input:

45,8

Sample Output:

37

Solution:

value, base = input().split(',')
result = int(value.strip(), int(base.strip()))
print(result)

Problem 3: Counting Digits and Lowercase Letters in a String

Score: 10

Input a string and count the number of digit characters and lowercase alphabetic characters.

Input Format:

A single line containing an arbitrary string.

Output Format:

A line in the format: 共有?个数字,?个小写字符 where the question marks are relpaced by the respective counts.

Sample Input:

helo134ss12

Sample Output:

共有5个数字,6个小写字符

Solution:

s = input()
digit_count = 0
lower_count = 0
for ch in s:
    if '0' <= ch <= '9':
        digit_count += 1
    elif 'a' <= ch <= 'z':
        lower_count += 1
print(f'共有{digit_count}个数字,{lower_count}个小写字符')
Tags: Python

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.