Tag Archives: Python

How to do exception handling in python?

By | January 29, 2024

Exception handling in Python is done using the try, except, else, and finally blocks. Here’s a basic structure of a try-except block: Let’s break down the components: Example with a specific exception: In this example: You can customize exception handling based on the specific requirements of your code. It’s generally a good practice to catch… Read More »

What are python name spaces?

By | January 27, 2024

In Python, a namespace is a container that holds a set of names (identifiers) and their corresponding objects. It serves as a mapping between names and objects, allowing you to organize and manage the scope of variables, functions, classes, and other identifiers in your code. Namespaces help avoid naming conflicts and provide a way to… Read More »

Concurrency in Python

By | January 18, 2024

In Python, there are several ways to implement concurrency, allowing you to execute multiple tasks concurrently to improve the performance of your code. Here are some commonly used methods: Threading: Multiprocessing: Asyncio (asynchronous I/O): ThreadPoolExecutor and ProcessPoolExecutor: Choose the concurrency approach that best fits your specific use case, considering factors such as the nature of… Read More »

Setting button actions programatically in Java, Python, JavaScript, SWIFT and Kotlin

By | January 9, 2024

Button action calls for an action when user tap on a button. This may be for making a service call, for doing some calculation or simply just to dismiss a view. Here are the different ways of setting button action in different languages. Java Python (TKinter) JavaScript: Swift Kotlin These examples demonstrate how to set… Read More »

Three common errors in Python programming along with examples

By | January 5, 2024

1. Syntax ErrorExample: 2. IndentationErrorExample: Explanation: Python relies on indentation to define block structures. The print statement is not properly indented under the if statement, leading to an indentation error. 3.NameErrorExample: Explanation: The variable y is not defined before trying to print it, resulting in a NameError. It’s worth noting that the examples provided are… Read More »

Convert a date from Central Standard Time (CST) to Indian Standard Time (IST)

By | January 2, 2024

To convert a date from Central Standard Time (CST) to Indian Standard Time (IST), you need to consider the time zone difference between these two zones. Below are examples in Swift : Swift: Java: Python: JavaScript: These examples assume a specific date and time format. You should adjust the date format and time zone identifiers… Read More »

Converting date into string format in Python, Javascript, Java and Ruby

By | December 30, 2023

Pythonfrom datetime import datetimedate_object = datetime.now()date_string = date_object.strftime(“%Y-%m-%d %H:%M:%S”)print(date_string) JavaScriptconst currentDate = new Date();const dateString = currentDate.toISOString(); // Adjust the format as neededconsole.log(dateString); Javaimport java.text.SimpleDateFormat;import java.util.Date; public class DateToString {public static void main(String[] args) {Date currentDate = new Date();SimpleDateFormat dateFormat = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);String dateString = dateFormat.format(currentDate);System.out.println(dateString);}} Rubyrequire ‘date’current_date = DateTime.nowdate_string = current_date.strftime(‘%Y-%m-%d %H:%M:%S’)puts date_string… Read More »

Problem: There are N prisoners standing in a circle, waiting to be executed…

By | June 27, 2023

There are N prisoners standing in a circle, waiting to be executed. The executions are carried out starting with the kth person, and removing every successive kth person going clockwise until there is no one left. Given N and k, write an algorithm to determine where a prisoner should stand in order to be the… Read More »

Given a sorted array, find the smallest positive integer that is not the sum of a subset of the array.

By | June 27, 2023

Java public class SmallestPositiveInteger {    public static int findSmallestPositiveInteger(int[] nums) {        int smallest = 1;        for (int num : nums) {            if (num <= smallest) {                smallest += num;            } else… Read More »