Lakshmi Srinivas has Published 315 Articles

Java program to generate and print Floyd’s triangle

Lakshmi Srinivas

Lakshmi Srinivas

Updated on 13-Mar-2020 07:22:18

4K+ Views

Floyd's triangle, named after Robert Floyd, is a right-angled triangle, which is made using natural numbers. It starts at 1 and consecutively selects the next greater number in the sequence.AlgorithmTake a number of rows to be printed, n.Make outer iteration I for n times to print rowsMake inner iteration for ... Read More

Java program to print ASCII value of a particular character

Lakshmi Srinivas

Lakshmi Srinivas

Updated on 13-Mar-2020 07:06:25

466 Views

ASCII stands for American Standard Code for Information Interchange. There are 128 standard ASCII codes, each of which can be represented by a 7-digit binary number: 0000000 through 1111111.If you try to store a character into an integer value it stores the ASCII value of the respective character.Exampleimport java.util.Scanner; public ... Read More

Java program to find the average of given numbers using arrays

Lakshmi Srinivas

Lakshmi Srinivas

Updated on 13-Mar-2020 06:56:23

3K+ Views

You can read data from the user using scanner class.Using the nextInt() method of this class get the number of elements from the user.Create an empty array.Store the elements entered by the user in the array created above.Finally, Add all the elements in the array and divide the sub by ... Read More

Java program to find the smallest number in an array

Lakshmi Srinivas

Lakshmi Srinivas

Updated on 13-Mar-2020 06:10:15

14K+ Views

To find the smallest element of the given array, first of all, sort the array.Sorting an arrayCompare the first two elements of the arrayIf the first element is greater than the second swap them.Then, compare 2nd and 3rd elements if the second element is greater than the 3rd swap them.Repeat ... Read More

Java program to implement binary search

Lakshmi Srinivas

Lakshmi Srinivas

Updated on 13-Mar-2020 05:52:42

2K+ Views

Binary search is a fast search algorithm with run-time complexity of Ο(log n). This search algorithm works on the principle of divide and conquer. For this algorithm to work properly, the data collection should be in the sorted form.The binary search looks for a particular item by comparing the middle most ... Read More

How to Humanize numbers with Python?

Lakshmi Srinivas

Lakshmi Srinivas

Updated on 05-Mar-2020 10:59:25

219 Views

If you want something that converts integers to words like 99 to ninety-nine, you have to use an external package or build one yourself. The pynum2word module is pretty good at this task. You can install it using −$ pip install pynum2wordThen use it in the following way −>>> import ... Read More

How to generate all permutations of a list in Python?

Lakshmi Srinivas

Lakshmi Srinivas

Updated on 05-Mar-2020 10:20:55

345 Views

You can use the itertools package's permutations method to find all permutations of a list in Python. You can use it as follows −Exampleimport itertools perms = list(itertools.permutations([1, 2, 3])) print(perms)OutputThis will give the output −[(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]

How to create a lambda inside a Python loop?

Lakshmi Srinivas

Lakshmi Srinivas

Updated on 05-Mar-2020 09:54:01

1K+ Views

You can create a list of lambdas in a python loop using the following syntax −Syntaxdef square(x): return lambda : x*x listOfLambdas = [square(i) for i in [1, 2, 3, 4, 5]] for f in listOfLambdas: print f()OutputThis will give the output −1 4 9 16 25You can also achieve ... Read More

What are common Python programming mistakes programmers do?

Lakshmi Srinivas

Lakshmi Srinivas

Updated on 05-Mar-2020 07:57:14

71 Views

Here are some of the most common python programming mistakes/gotchas that programmers commit −Scope name lookups − Python follows scoping rules in order of LEGB(Local, Enclosing, Global, Built-in). Since python has no strict type binding, programmers can reassociate an outer scope variable to another value that might be used in ... Read More

How to overload python ternary operator?

Lakshmi Srinivas

Lakshmi Srinivas

Updated on 05-Mar-2020 07:38:52

237 Views

The ternary operator cannot be overloaded. Though you can wrap it up in a lambda/function and use it. For exampleresult = lambda x: 1 if x < 3 else 10 print(result(2)) print(result(1000))OutputThis will give the output −1 10

Advertisements