Eliminate Numbers in a String in Python

Arjun Thakur
Updated on 05-Mar-2020 11:25:01

222 Views

You can create an array to keep track of all non digit characters in a string. Then finally join this array using "".join method. examplemy_str = 'qwerty123asdf32' non_digits = [] for c in my_str:    if not c.isdigit():       non_digits.append(c) result = ''.join(non_digits) print(result)OutputThis will give the outputqwertyasdfexampleYou can also achieve this using a python list comprehension in a single line. my_str = 'qwerty123asdf32' result = ''.join([c for c in my_str if not c.isdigit()]) print(result)OutputThis will give the outputqwertyasdf

Find Keith Numbers Using Python

Priya Pallavi
Updated on 05-Mar-2020 11:23:28

831 Views

You can use the following code to find if a number is a keith number in python −Exampledef is_keith_number(n):    # Find sum of digits by first getting an array of all digits then adding them    c = str(n)    a = list(map(int, c))    b = sum(a)    # Now check if the number is a keith number    # For example, 14 is a keith number because:    # 1+4 = 5    # 4+5 = 9    # 5+9 = 14    while b < n:       a = a[1:] + [b]       b = sum(a)    return (b == n) & (len(c) > 1) print(is_keith_number(14))OutputThis will give the output −True

Print Objects in Array using Java Method

Every ; Thing
Updated on 05-Mar-2020 11:21:10

191 Views

Exampleclass Shape{     private String shapeName;     private int numSides;      Shape(String shapeName, int numSides){         this.shapeName = shapeName;         this.numSides = numSides;     }     public String toString(){         return shapeName + " has " + numSides + " sides.";     } } class ObjectList{     private Object[] list = new Object[10];     private int numElement = 0;      public void add(Object next){         list[numElement] = next;         numElement++;     }      @Override     public String toString(){         String str="";         int i=0;         while(list[i] != null){             str += list[i]+"";             i++;         }         return str;     } } public class Driver{     public static void main(String[] args){         ObjectList list = new ObjectList();         Shape square = new Shape("square", 4);         Shape hex = new Shape("hexagon", 6);         list.add(hex);         list.add(square);         System.out.println(list);     } }

Queue Emplace in C++ STL

Sunidhi Bansal
Updated on 05-Mar-2020 11:17:40

925 Views

In this article we will be discussing the working, syntax and examples of queue::emplace() function in C++ STL.What is a queue in C++ STL?Queue is a simple sequence or data structure defined in the C++ STL which does insertion and deletion of the data in FIFO(First In First Out) fashion. The data in a queue is stored in continuous manner. The elements are inserted at the end and removed from the starting of the queue. In C++ STL there is already a predefined template of queue, which inserts and removes the data in the similar fashion of a queue.What is ... Read More

Add and Subtract Large Numbers Using Python

karthikeya Boyini
Updated on 05-Mar-2020 11:15:16

2K+ Views

You can add/subtract large numbers in python directly without worrying about speed. Python supports a "bignum" integer type which can work with arbitrarily large numbers. In Python 2.5+, this type is called long and is separate from the int type, but the interpreter will automatically use whichever is more appropriate.As long as you have version 2.5 or better, just perform standard math operations and any number which exceeds the boundaries of 32-bit math will be automatically (and transparently) converted to a bignum.examplea = 182841384165841685416854134135 b = 135481653441354138548413384135 print(a - b)OutputThis will give the output −47359730724487546868440750000

Deque Shrink to Fit in C++ STL

Sunidhi Bansal
Updated on 05-Mar-2020 11:14:14

318 Views

In this article we will be discussing the working, syntax and examples of deque::shrink_to_fit() function in C++ STL.What is Deque?Deque is the Double Ended Queues that are the sequence containers which provides the functionality of expansion and contraction on both the ends. A queue data structure allow user to insert data only at the END and delete data from the FRONT. Let’s take the analogy of queues at bus stops where the person can be inserted to a queue from the END only and the person standing in the FRONT is the first to be removed whereas in Double ended ... Read More

Find Kaprekar Numbers within a Given Range using Python

Nikitha N
Updated on 05-Mar-2020 11:13:49

1K+ Views

A modified Kaprekar number is a positive whole number n with d digits, such that when we split its square into two pieces - a right hand piece r with d digits and a left hand piece l that contains the remaining d or d−1 digits, the sum of the pieces is equal to the original number (i.e. l + r = n).You can find Kaprekar numbers within a given range by testing each number for the given condition in the given range. exampledef print_Kaprekar_nums(start, end):    for i in range(start, end + 1):       # Get the digits ... Read More

Generate Pyramid of Numbers Using Python

Ankith Reddy
Updated on 05-Mar-2020 11:11:42

938 Views

There are multiple variations of generating pyramids using numbers in Python. Let's look at the 2 simplest formsExamplefor i in range(5):    for j in range(i + 1):       print(j + 1, end="")    print("")OutputThis will give the output1 12 123 1234 12345ExampleYou can also print numbers continuously usingstart = 1 for i in range(5):    for j in range(i + 1):       print(start, end=" ")       start += 1 print("")OutputThis will give the output1 2 3 4 5 6 7 8 9 10 11 12 13 14 15ExampleYou can also print these numbers in reverse usingstart = 15 for i in range(5):    for j in range(i + 1):    print(start, end=" ")    start -= 1 print("")OutputThis will give the output15 14 13 12 11 10 9 8 7 6 5 4 3 2 1

Push Front in C++ STL Deque

Sunidhi Bansal
Updated on 05-Mar-2020 11:10:59

2K+ Views

In this article we will be discussing the working, syntax and examples of deque::push_front() function in C++ STL.What is Deque?Deque is the Double Ended Queues that are the sequence containers which provides the functionality of expansion and contraction on both the ends. A queue data structure allow user to insert data only at the END and delete data from the FRONT. Let’s take the analogy of queues at bus stops where the person can be inserted to a queue from the END only and the person standing in the FRONT is the first to be removed whereas in Double ended ... Read More

Handle Very Large Numbers in Python

Abhinanda Shri
Updated on 05-Mar-2020 11:08:24

13K+ Views

You can perform arithmetic operations on large numbers in python directly without worrying about speed. Python supports a "bignum" integer type which can work with arbitrarily large numbers. In Python 2.5+, this type is called long and is separate from the int type, but the interpreter will automatically use whichever is more appropriate.As long as you have version 2.5 or better, just perform standard math operations and any number which exceeds the boundaries of 32-bit math will be automatically (and transparently) converted to a bignum.examplea = 182841384165841685416854134135 b = 135481653441354138548413384135 print(a - b)OutputThis will give the output −47359730724487546868440750000

Advertisements