Team Ai
Apppublic

Vihang28/Mathematical_and_string_Operations

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
app.py180 linesDownload Raw Back to root
1import streamlit as st2 3description1 = "Given two integer numbers, return their product only if the product is equal to or lower than 1000. Otherwise, return their sum."4description2 = "Iterate the first 10 numbers, and in each iteration, returns the sum of the current and previous number."5description3 = "Display characters that are present at an even index number from the provided string."6description4 = "Remove the characters from a string starting from zero up to n and returns a new string."7description5 = "Returns the number of times a substring is present in the provided string."8description6 = "Checks if a number is Palindrom or Not."9description7 = "Given two list of numbers, returns a new list such that the new list contains odd numbers from the first list and even numbers from the second list."10description8 = "Returns multiplication table of the provided number range."11description9 = "Returns an integer value of base raises to the power of exponent."12 13def sum_of_two(a,b):14    prod = a*b15    sum1 = a+b16    if prod <= 1000 :17        return(prod)18    else:19        return(sum1)20 21def start_iteration(a):22    num_set = '''23                Showning current and previous number sum in a range(10): \n'''24    temp = a25    for i in range(a,a+10):26        if temp == a:27            num_set = num_set +f''' 28            Current Number {temp} Previous Number 0 Sum: {temp} '''29        else:30            num_set = num_set + f''' 31            Current Number {temp} Previous Number {temp-1} Sum: {temp+temp-1} '''32        temp = temp + 133    return(num_set)34 35def even_index(str1):36    str_lst = []37    for i in range(len(str1)):38        if i % 2 == 0 :39           str_lst.append(str1[i]) 40    return(str_lst)41 42def change_str(str2,num2):43    new_srt = ""44    for i in range(num2,len(str2)):45        new_srt = new_srt + str2[i]46    return new_srt47 48def str_count(str3,sub_str):49    new_str3 = str3.lower()50    new_sub_str = sub_str.lower()51    x = new_str3.count(new_sub_str)52    return x53 54def check_palin(check_num):55    num_str = str(check_num)56    if num_str == num_str[::-1] :57        return("Palindrom")58    else:59        return("Not Palindrom")60 61def create_new(num_str1,num_str2):62    fin_lst = []63    num_lst1 = num_str1.split()64    num_lst2 = num_str2.split()65    for x in num_lst1:66        if int(x) % 2 == 1:67            fin_lst.append(int(x))68    for y in num_lst2:69        if int(y) % 2 == 0:70            fin_lst.append(int(y))71    return(fin_lst)72 73def create_tbl(tbl1,tbl2):74    list_of_tbl = []75    if tbl1<tbl2:76        for i in range(tbl1,tbl2+1):77            table_lst = []78            for j in range(1,11):79                table_lst.append(j*i)80            list_of_tbl.append(table_lst)81        return(list_of_tbl)82    elif tbl2<tbl1:83        for i in range(tbl2,tbl1+1):84            table_lst = []85            for j in range(1,11):86                table_lst.append(j*i)87            list_of_tbl.append(table_lst)88        return(list_of_tbl)89    else:90        for j in range(1,11):91            list_of_tbl.append(tbl2*j)92        return(list_of_tbl)93        94 95def find_exp(base1,exp1):96    if exp1 < 0 :97        exp1 = exp1 * (-1)98    return base1**exp199    100 101st.markdown("<h3 style='text-align:center; font-size:24px;'>Mathematical and String Operations</h3>", unsafe_allow_html=True)102st.write("---")103radio_bar = st.sidebar.radio(options=[description1, description2, description3, description4, description5, description6, description7, description8, description9], label="Select from the below mentioned operations:")104if radio_bar == description1:105    st.markdown("<h6 style='text-align:center; font-size:16px;'>"+description1+"</h6>", unsafe_allow_html=True)106    first_num = st.number_input(label="Enter First Number:",value=0)107    second_num = st.number_input(label="Enter Second Number:",value=0)108    all_data = st.button("Calculate result")109    if all_data:110        result = sum_of_two(first_num,second_num)111        st.success(result)112 113if radio_bar == description2 :114    st.markdown("<h6 style='text-align:center; font-size:16px;'>"+description2+"</h6>", unsafe_allow_html=True)115    first_num = st.number_input(label="Enter Starting Number:",value=0)116    all_data = st.button("Show result")117    if all_data:118        result = start_iteration(first_num)119        st.success(result)120 121if radio_bar == description3 :122    st.markdown("<h6 style='text-align:center; font-size:16px;'>"+description3+"</h6>", unsafe_allow_html=True)123    str1 = st.text_input(label="Enter the string:",value="")124    all_data = st.button("Show result")125    if all_data:126        result = even_index(str1)127        st.success(result)128 129if radio_bar == description4 :130    st.markdown("<h6 style='text-align:center; font-size:16px;'>"+description4+"</h6>", unsafe_allow_html=True)131    str2 = st.text_input(label="Enter the string:",value="")132    char_num = st.number_input(label="Number of characters to remove:",value=0)133    all_data = st.button("Show result")134    if all_data:135        result = change_str(str2,char_num)136        st.success(result)137 138if radio_bar == description5 :139    st.markdown("<h6 style='text-align:center; font-size:16px;'>"+description5+"</h6>", unsafe_allow_html=True)140    str3 = st.text_input(label="Enter the string:",value="")141    sub_str= st.text_input(label="Enter the sub-string:",value="")142    all_data = st.button("Show result")143    if all_data:144        result = str_count(str3,sub_str)145        st.success(f"The sub-string is present {result} times in the string.")146 147if radio_bar == description6 :148    st.markdown("<h6 style='text-align:center; font-size:16px;'>"+description6+"</h6>", unsafe_allow_html=True)149    check_num = st.number_input(label="Enter Number:",value=0)150    all_data = st.button("Check")151    if all_data:152        result = check_palin(check_num)153        st.success(result)154 155if radio_bar == description7 :156    st.markdown("<h6 style='text-align:center; font-size:16px;'>"+description7+"</h6>", unsafe_allow_html=True)157    num_str1 = st.text_input(label="Enter the numbers of first list and separate each number by single space(' '):",value=0)158    num_str2 = st.text_input(label="Enter the numbers of second list and separate each number by single space(' '):",value=0)159    all_data = st.button("Show Result")160    if all_data:161        result = create_new(num_str1,num_str2)162        st.success(result)163 164if radio_bar == description8 :165    st.markdown("<h6 style='text-align:center; font-size:16px;'>"+description8+"</h6>", unsafe_allow_html=True)166    start_num8 = st.number_input(label="Enter start Number:",value=0)167    end_num8 = st.number_input(label="Enter end Number:",value=0)168    all_data = st.button("Show Result")169    if all_data:170        result = create_tbl(start_num8,end_num8)171        st.success(result)172 173if radio_bar == description9 :174    st.markdown("<h6 style='text-align:center; font-size:16px;'>"+description9+"</h6>", unsafe_allow_html=True)175    base_num = st.number_input(label="Enter Base Number:",value=0)176    exp_num = st.number_input(label="Enter Exponent:",value=0)177    all_data = st.button("Calculate")178    if all_data:179        result = find_exp(base_num,exp_num)180        st.success(result)