Skip to main content

Posts

Count occurrences of a character in string in Python

Hello everyone! Today we are going to see how can we count the occurrences of a character in a given string using Python.

Python provides a library method count which can be used to count the occurrence of a substring in a given string. Below code snippet takes a character as a substring and returns the count of that in the source string.
# Enter the string in which we need to search sentence=input("Enter a string\n")# Enter a string/character which needs to be searchedsub_string=input("Enter the substring to search\n")# Getting the count of the sub string in source string - convert both to lowercase if we want to count irrespective of casecount_of_substring=sentence.lower().count(sub_string.lower())# Print the count of the substring in the source stringprint('Count of '+sub_string+' in '+sentence+' is '+str(count_of_substring)) The output of this program will be as follows -
>> Enter a string Help is always given at Hogwarts to those w…
Recent posts

Remove duplicates from an array (JavaScript)

In this post, we will see how to remove duplicates from an array in JavaScript.

We will look into two methods - ES6 and Vanilla JavaScript.
ES6 Method In ES6, this is very simple using the Set constructor and the Spread syntax as below - letarray=[1,3,4,1,2,4,3,4,5,7,6,5,8];letunique=[...newSet(array)];console.log(unique); The output of this code will be -
[1,3,4,2,5,7,6,8]Set - lets you store unique values of any type, whether primitive values or object references.
Spread - allows an iterable to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected, or an object expression to be expanded in places where zero or more key-value pairs (for object literals) are expected.
Using Vanilla JavaScript If you want to use vanilla JavaScript, you can utilize Array.indexOf() and Array.filter() methods as follows - varsandwiches=["turkey","ham","turkey","tuna","pb&j","ham",&q…

Material design profile page in Android

Hey everyone, some days back I was working on one my personal Android project. In that project, I was supposed to create a simple profile page for a user. This profile page was supposed to show some basic details of a user.

The output of this UI will be like this -
I created the profile page using material design and in this post, I am going to discuss a step by step tutorial to create a simple yet elegant profile page. Without further ado, let's get started.
Creating a new project Click on File ➤ New Project ➤ Empty Activity and fill the necessary details. Change styles.xml fileNavigate to app\src\main\res\values\styles.xmlChange the style value from DarkActionBar to NoActionBar as below<resources><!-- Base application theme. --><stylename="AppTheme"parent="Theme.AppCompat.Light.NoActionBar"><!-- Customize your theme here. --><itemname="colorPrimary">@color/colorPrimary</item><itemname="colorPrimaryDark&qu…

Grokking Computer Science

Computers are fun, aren't they? Today we see computers in every aspect of our lives but it is even more fun to know how computers work.

Computer Science is the study of how computers work. The study has a theoretical and mathematical focus and involves processes such as Algorithms, to solve problems. Why should we study Computer Science? The most important aspect of computer science is problem-solving. Problem solving is an essential skill for life. Students study the design, development and analysis of software and hardware used to solve problems in a variety of business, scientific and social contexts. Because computers solve problems to serve people, there is a significant human side to computer science as well.
There are a few reasons which explain the benefits of computer science - Computing is part of everything we do.It helps in solving complex and challenging problems.It enables us to make positive changes in the world (to make the world a better place - a clich├ęd line, bu…

Types of variables in Java

What is a variable?
A variable is a piece of memory which stores a data value. Thus, a variable has a data type. Primarily there are eight primitive data types in java. We will learn about these in a later post.
Variables are typically used to store information which our Java program needs to do its job. This can be any kind of information ranging from texts, codes (e.g. country codes, currency codes etc.) to numbers, temporary results of multi-step calculations etc.
You can declare a variable as below -  datatypename[initialization]; Initialization of a variable is not mandatory in all cases.
Types of variables There are three types of variables in Java. We will discuss them one by one. 1. Local variables - They can be declared in a method, constructor, or block. When a method is entered, an area is pushed onto the call stack which contains slots for each local variable and parameter. When the method is called, the parameter slots are initialized to the parameter values.When the meth…

Number System Conversions

When we start learning mathematics, the first thing we learn is Numbers (0, 1, 2, 3, ...). Basically, numbers are a set of values used to represent different quantities. For example, we can represent the number of students in a class, the number of viewers watching the soccer world cup final etc.

The numbers we use most frequently are in Decimal Number System. Similarly, there are other number systems which have different digits/characters to represent the quantities.

Decimal Number System - 10 digits (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)Binary Number System - 2 digits (0, 1)Octal Number System - 8 digits (0, 1, 2, 3, 4, 5, 6, 7)Hexadecimal Number System - 16 digits (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F) You can learn more about the number systems here.
Sometimes, it is required to convert decimal numbers to one of the other number systems and vice versa. Therefore, in this blog post, we will look into the different conversion mechanisms. Though there are different APIs in Java tha…