Sunday, February 7, 2021

Linear Search Algorithm | With Example Code

Linear Search is a simplest searching algorithm that goes through all the elements in sequence from start to end. In linear search, the algorithm checks every single element whether it’s a required element or not.

This searching technique can be applied to sorted or unsorted lists or arrays.

For Example:

Let us consider an unsorted array of size 9. The following image consists of the elements in the array. Now we need to find the exact position/place of element “20”.

The algorithm is as follows:

Start searching from the leftmost element of arr[] and one by one compare x with each element of arr[]
If x matches with an element, return the index.
If x doesn’t match with any of the elements, return -1.

Program Code

#include <stdio.h> 
int search(int arr[], int n, int x)
{
int i;
for (i = 0; i < n; i++)
if (arr[i] == x)
return i;


return -1;


}



int main(void)


{


int arr[] = { 9, 4, 45, 75, 16, 15, 23, 54, 79 };


int x = 23;


int n = sizeof(arr) / sizeof(arr[0]);


int result = search(arr, n, x);


if(result == -1)


printf("Element not present.");


else


printf("Element is present at index %d", result); return 0;


}

Output:  


Element is present at index 6.
The Time Complexity of Linear Search Algorithm is O(n). That means the time taken to search will directly be dependent on the size of the array or list. Nowadays, in a practical scenario, Linear search is rarely used. Because of some faster searching algorithms discovered like, Binary Search and Hashing Tables, which eventually takes lesser time to search any data from arrays or lists.




Read More

Friday, October 4, 2019

Python IDEs and Code Editors - Basic Guide and Top picks

The hugely popular Programming language Python was developed in 1991 to code languages used for several purposes like:

Python IDE and Code editors - basic guide and top picks

  • Back-end web and mobile app development
  • Desktop app 
  • Software development
  • To write system scripts

Python is the leader in the coding business because of its easy English syntax, easy to learn framework user-friendly interface. It is used for server web development, software development, and Artificial intelligence. Python works on many platforms and to know Python you should know more about the best Python code editors and IDE’s.

What is IDE and code editor?

IDE's or Integrated development environments are those programs that amalgamate the tools for writing and testing software, websites and mobile-based apps on its platform. They typically include:


  • Source code editor: This is a text editor that is used for editing programming languages.
  • Building tools for automation: To automate the software development process and reduce manual tasks.
  • Debugger: This is a tool that examines codes for errors and also aids to diagnose different problems. 



With IDE, one can also automate the work of developers which results in a significant reduction of manual work and therefore, errors caused due to it. It also facilitates the SDLC (software development life cycle) process further by reducing the time taken for development, testing and debugging.

But, if you have to write only the code, then why use the IDE platform? 

Instead, you can rely on less-resourced intensive code editors which can be written in plain text editors. Code editors are well equipped and have features specific to writing codes which can benefit your SDLC process and amp up the speed with a notable reduction in time, effort and errors.

We have handpicked top 10 python IDE’s and code editors to help you select the most ideal one suited for your project:

Idle –IDE

Idle is the best way to start for beginners. It has by default the installs of the language. Idle is great for beginners because it is light-weight and capable of doing many things. But with more complications, later on, Idle might not be the best fit. It is open source and comes free of cost.

  • User-friendly and best for beginners
  • Presence of smart indentation 
  • Highlights syntax
  • Open-source

Thonny –IDE

Thonny is yet another beginner-friendly python IDE that was developed by the Institute of Computer Science at the University of Tartu in Estonia and aimed at Python beginners. This like Idle is open-source and is free of cost too.

  • Open-source
  • User interface is simple
  • Simplified debugger
  • User logs maintained to keep a tab on the entire coding process

PyCharm – IDE

Mainly for intermediate and advance python users, it is the best extension to your Idle days. Charm comes in a two-tier pricing mode where the first is free of cost limited community version and the second tier is the fully paid professional version.

  • Apt for professional use
  • Error highlighting and suggestion fixing
  • A smart code navigation system
  • Strong debugging

Spyder – IDE

Mainly used by python scientists designed with data science. Its main focus is integrating with python data libraries which makes it a perfect choice for dedicated data science needs. This being Open source is free to explore.

  • Specializes in Data science
  • Python code auto-completion
  • Syntax highlight

Sublime Text – Code editor

Priced at $80 this versatile code editor supports all languages including python. All the needed basic code editing features are offered here which can be extended further with plugins like debugging and Django integration.

  • Fully features and has everything one is looking for
  • GOTO search function
  • Out of box highlighting of syntax

Repl.it (IDE) 

This is a general-purpose IDE that supports python and even lets the developers’ code directly through the browser. It is an open-source format that comes free of cost. With this tool, all the developer needs to write down an efficient python code is to have a system with a good internet connection.

  • Code-error analysis
  • Ability to code through the browser
  • Goes with other plugins

Visual Studio (IDE)

This is a well-packaged IDE that comes from the house of Microsoft. It is also python compatible with the help of PTVS (Python tools visual studio). Good for complicated applications and will cost the exchequer a neat $45 per month.

  • Apt for multiple languages
  • Can add python compatibility with the help of plugins
  • Have great navigation tools and debugging options
  • Has the support for Microsoft

Visual Studio Code – Code Editor

Visual Studio Code is free of cost and can be configured easily with extensions and plugins. This comes with syntax highlighting, debugging and an auto-completion feature called ‘Intellisense'.

  • Syntax highlighting
  • Debugger
  • Rich in feature though lightweight

Vim – Code Editor

This has a pre-installed Mac OS and UNIX OS. It is a basic editor but has the option for plugins and extensions.

  • Open-source
  • Easily configured with python
  • Compatible with plugins for syntax highlighting, code auto-completion and debugging

Atom – Code Editor

Flexible, lightweight and has its features like code auto-completion, adjustable user interface and debugging.

  • Open-source
  • Customized user interface
  • Find and replace the search option

You can choose a suitable Python IDE and Code Editor that helps make your software development process easier without any hassle. Let us know in the comments section if you know some other best code editors or IDEs suitable for easy to use, simple and hassle-free development.

Recommended Articles:

Read More

Monday, September 30, 2019

Python String split() method (With Examples)

split() method is used to break up a bigger string into a smaller list of strings. We need to specify a separator(default separator is a whitespace if not specified) to help in splitting of the string. As a result we get a list where each word is a separate list item. In other words split() is used to split a string into a list.

Syntax

str.split(separator, maxsplit)

Parameters


separator
This is any delimiter which is used to split the string. If it is not provided any whitespace(space, newline etc.) is considered as default.
maxsplit

It is a numeric optional value which specifies how many splits will be performed. If maxsplit is not provided then there is no limit. If maxsplit is not specified or -1(all occurrences), then there is no limit on the number of splits (all possible splits will be made).
Note: When maxsplit is specified, the list will contain the specified number of elements plus one.

Returns

Returns a list of strings which has been split by the specified separator. The split() splits the string at the separator and returns a list of strings.

Below examples shows Python split string by comma, python split string by character and python split string by space.

Example 1

textstring = 'Coding is Fantastic'
# Split() at space 
print(textstring.split()) 
wordstring = 'Coding, is, Fantastic'
# Split() at ',' 
print(wordstring.split(', ')) 
wordstring = 'Coding#is#Fantastic'
# Splitting at '#' 
print(wordstring.split('#')) 
Output


['Coding', 'is', 'Fantastic']
['Coding', 'is', 'Fantastic']
['Coding', 'is', 'Fantastic']

Example 2 


wordstring = 'Apple, Orange, Grapes, Kiwi'
# maxsplit: 0 
print(wordstring.split(', ', 0)) 
# maxsplit: 3 
print(wordstring.split(', ', 3)) 
# maxsplit: 1 
print(wordstring.split(', ', 1)) 
Output

['Apple, Orange, Grapes, Kiwi']
['Apple', 'Orange', 'Grapes', 'Kiwi']
['Apple', 'Orange, Grapes, Kiwi']

Further Read:

Read More

Monday, September 16, 2019

Python String - isdigit() method (With Examples)

isdigit() is an in-built method in python which is used for string manipulations. isdigit() returns True if all characters in the string are digits, else it returns False. This Python String Method does not take any parameters.

Syntax

string.isdigit()

Returns

True - If all the characters in the string are digits.
False - If 1 or more non-digit characters are present in string.

Example 1

# checking for digit using isdigit
str = '987654321'
print(str.isdigit())

str = 'Houseno22'
print(str.isdigit())

Output 
True
False

Example 2

# Checking for superscripts or subscripts with isdigit
str = '550\u2076'
print(str.isdigit())

Output 
True

Note: fractions, roman numerals are not considered digits, hence isdigit() returns False. This method will work with unsigned integers, superscripts or subscripts.

Further Read:

Read More

Thursday, September 12, 2019

Python Concatenate Strings | With Examples

What is concatenation?

Usually in any programming language, string concatenation means to join 2 strings together.  For example, if we have string1 = “Learn” and string2 = “Python”. The concatenation of string1 and string2 will give a result “Learn Python”. The contents of both items are merged by placing them side by side.

In this post we will learn how to do concatenation in python. In python strings are str objects. String objects are immutable.

Concatenation using ‘+’ Operator

This is the quickest way in Python to concatenate strings. You simply need to put ‘+’ in between various string variables and get a combined string as a result.

Example 1:


”Hello” + “ World” + “ !!” 
Output

“Hello World !!”

Note that spaces were included as part of string.

Example 2:


"help” * 5

Output

“helphelphelphelphelp”

This is another unique feature of Python. You can multiply a string and concatenate it as many times as you want.

Example 3:


str1 = “Learn”
str2 = “ Python”
str1 + str2

Output

“Learn Python”


str3 = str1 + str2
Print str3

Output

“Learn Python”

Many programming languages do implicit conversions when you try to concatenation between string and non string data. In Python however you will see a TypeError.

Example 4:


“Learn Python” + 101


Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    "Learn Python" + 101
TypeError: cannot concatenate 'str' and 'int' objects

Concatenation using .join()

.join() is another useful way to do concatenation in Python. This works well with List which is made up of strings and you want to combine all the strings in this list to give you one big concatenated string result. .join() also takes a “joiner” which should be used in between the strings while joining.

Example 1:


str = ["How" , "Are" , "You"]
‘-’.join(str)

Output

‘How-Are-You’


So now you know how to concatenate strings in Python. There are many other string functions and methods available to do string data manipulations. We will keep on adding such useful information in the future. Bookmark/follow this blog to get the latest updates.

Further Read:

Read More

Saturday, September 7, 2019

Python String Length | Using len() Function

While programming in python we may need to find what is the length of a string?
Real world data validation may need that you add a check for a username of not more than size 30. Python provides a built-in function len() to find a string's length. len() takes string name as parameter and returns it's length.

Syntax


len(s)

s is the string of which we want to find the length.

Returns

len() returns the number of characters in a string. It is an integer value.

Example


s = "codefantastic"
print(len(s))

Output


13

Note: len() built-in function can also be used to find the number of items of an object passed. Not only string, len() function works with tuple, list, range or bytes also. In fact you can pass collection also to count the number of items present in it.

Further read:

Read More

Thursday, September 5, 2019

Python String Methods

String manipulations are an integral part of any programming language. Python is no different. The makers of python have provided a rich collection of built-in string methods to suit almost any kind of string processing. With the help of these python in-built methods you can trim, extract a substring, format, count, etc almost any string.

Below we have listed built-in python methods for strings. By clicking on each string method you can see how they can be used with examples.

 Python String Methods
 Description
casefold()
Python string method returns a casefold copy of string, used for caseless matching.        
capitalize()
Python string method returns a copy of string with first character capitalised and remaining in lowercase.
center()
Python string method returns string centered in a provided width.
count()
Python string method returns the number of occurrences of a substring in a given string.
encode()
Python string method returns encoded version of the given string.
endswith()
Python string method returns True if a string ends with the given suffix. 
expandtabs()
Python string method specifies the amount of space to be replaced with the “\t” symbol in the string.
find()
Python string method returns the lowest index of the substring if it is found in a given string.
format()
Python string method used for multiple substitutions and value formatting.
format_map()
Python string method used to return a dictionary key value.
index()
Python method searches an element in the list and returns its index.
isalnum()
Python string method checks if  all the characters in a string is alphanumeric or not.
Python string built-in method used for string handling.
isascii()
Python string method returns all characters in the string are ASCII or not.
isdecimal()
Python string method checks if all characters in the string are decimal characters.
Python string method determines whether the given character is a digit.
isidentifier()
Python string method checks whether a string is a valid identifier.
islower()
Python string method checks characters in the string are lowercase or not.
isnumeric()
Python string method determines whether all the characters of the string are numeric characters or not.
isprintable()
Python string method checks if all characters in the string are printable or the string is empty.
isspace()
Python string method checks if there are only whitespace characters in the string.
istitle()
Python string method determines if the string is a title cased string
isupper()
Python string method checks the given string is either in uppercase, or not.
join()
Python string method returns a string in which the elements of a sequence have been joined.
ljust()
Returns the string left justified.
lower()
Returns returns the lowercased string.
lstrip()
Python string method returns a copy of the string with leading characters are removed.
maketrans()
This static method returns a translation table usable for translate().
partition()
This method is used for splitting the string.
replace()
replace method is used for string substitution.
rfind()
This python method returns the highest index of the substring if found in given string.
rindex()
Python method returns the highest index of the substring inside the string, if found.
rjust()
This method returns the string right justified.
rpartition()
This method is used for splitting the string.
rsplit()
Python string method returns a list of strings after breaking it in smaller ones.
rstrip()
Python string returns a copy of the string with trailing characters removed.
This method returns a list of strings after breaking the given string by the specified separator.
splitlines()
Python method splits the string based on the lines.
startswith()
Checks if string starts with the given prefix.
strip()
Python string method returns a string with both leading and trailing characters removed.
swapcase()
Python method converts all uppercase characters to lowercase and vice versa.
title()
Converts a given string to titlecased.
translate()
Returns the string where each character has been mapped through the given translation table.
upper()
Returns a string in uppercase.
zfill()
Python string method returns a copy of the string left filled with ASCII '0'.




Read More