Building a Custom Password Generator in Python: A Comprehensive Guide

In today's digital age, security is paramount. One of the simplest yet crucial aspects of digital security is having strong passwords. In this blog post, we'll dive into creating a Python script that generates secure passwords based on user preferences. The source code is at the end of this post.

Understanding the Code:
Let's break down the code step by step: 

1. Imports: We start by importing the necessary modules string and secrets. string provides a collection of string constants, and secrets is used for generating secure random numbers suitable for managing data such as passwords.
 
import string
import secrets

HackerRank Python Solution - Search Algorithm - Ice Cream Parlor

  • Two friends like to pool their money and go to the ice cream parlor. They always choose two distinct flavors and they spend all of their money. 
  • Given a list of prices for the flavors of ice cream, select the two that will cost all of the money they have. 
  • Example. m = 6 cost = [1,3,4,5,6]
  • The two flavors that cost 1 and 5 meet the criteria. Using 1-based indexing, they are at indices 1  and 4. 
Function Description:
  • icecreamParlor has the following parameter(s): 
    • int m: the amount of money they have to spend 
    • int cost[n]: the cost of each flavor of ice cream 

Image Background Removal using Python

In the world of image processing and manipulation, one common task is removing backgrounds from images. This can be particularly useful for creating visually appealing graphics or preparing images for various applications. One tool that helps achieve this is REMBG, a Python library designed for background removal from images. In this blog post, we'll guide you through a simple Python program that utilizes REMBG to remove backgrounds from images

Setting Up the Environment:

Before we dive into the code, let's ensure we have the necessary Python libraries installed:

  1. rembg: A Python library that will handle the background removal for us.

    pip install rembg
  2. PIL (Python Imaging Library): A Python library for image processing.

    pip install pillow
  3. easygui: A GUI library for file selection.

    pip install easygui

LeetCode Solution - Problem Valid Parentheses

  • Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. 
  • An input string is valid if: 
    • Open brackets must be closed by the same type of brackets.
    • Open brackets must be closed in the correct order.
    • Every close bracket has a corresponding open bracket of the same type.
Example 1:

Input: s = "()"
Output: true

LeetCode Solution - Problem Longest Substring Without Repeating Characters

  • Given a string s, find the length of the longest substring without repeating characters.
Example 1:

Input: s = "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.

LeetCode Solution - Problem Repeated DNA Sequences

  • The DNA sequence is composed of a series of nucleotides abbreviated as 'A', 'C', 'G', and 'T'. 
    • For example, "ACGAATTCCG" is a DNA sequence.
  • When studying DNA, it is useful to identify repeated sequences within the DNA.
  • Given a string s that represents a DNA sequence, return all the 10-letter-long sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in any order.
Example 1:

Input: s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
Output: ["AAAAACCCCC","CCCCCAAAAA"]

LeetCode Solution - Problem Longest Common Prefix

  • Write a function to find the longest common prefix string amongst an array of strings.
  • If there is no common prefix, return an empty string "".
Example 1:

Input: strs = ["flower","flow","flight"]
Output: "fl"

LeetCode Solution - Problem Reverse String

  • Write a function that reverses a string. The input string is given as an array of characters s.
  • You must do this by modifying the input array in-place with O(1) extra memory.
Example 1:

Input: s = ["h","e","l","l","o"]
Output: ["o","l","l","e","h"]

LeetCode Solution - Problem Pow(x, n)

  • Implement pow(x, n), which calculates x raised to the power n (i.e., x^n).
Example 1:

Input: x = 2.00000, n = 10
Output: 1024.00000

LeetCode Solution - Problem Factorial Trailing Zeroes

  • Given an integer n, return the number of trailing zeroes in n!.
  • Note that n! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1.
Example 1:

Input: n = 3
Output: 0
Explanation: 3! = 6, no trailing zero.

LeetCode Solution - Problem Palindrome Number

  • Given an integer x, return true if x is a palindrome, and false otherwise. 
Example 1:

Input: x = 121
Output: true
Explanation: 121 reads as 121 from left to right and from right to left.

LeetCode Solution - Problem Reverse Integer

  • Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0.
  • Assume the environment does not allow you to store 64-bit integers (signed or unsigned).
Example 1:

Input: x = 123
Output: 321

LeetCode Solution - Problem Single Number

  • Given a non-empty array of integers nums, every element appears twice except for one. Find that single one.
  • You must implement a solution with a linear runtime complexity and use only constant extra space.
Example 1:

Input: nums = [2,2,1]
Output: 1

LeetCode Solution - Problem Fizz Buzz

Given an integer n, return a string array answer (1-indexed) where:
  • answer[i] == "FizzBuzz" if i is divisible by 3 and 5.
  • answer[i] == "Fizz" if i is divisible by 3.
  • answer[i] == "Buzz" if i is divisible by 5.
  • answer[i] == i (as a string) if none of the above conditions are true.
Example 1:

Input: n = 3
Output: ["1","2","Fizz"]

HackerRank Python Solution - Regex and Parsing - Validating Roman Numerals

  • You are given a string, and you have to validate whether it's a valid Roman numeral. If it is valid, print True. Otherwise, print False. Try to create a regular expression for a valid Roman numeral.

HackerRank Python Solution - Regex and Parsing - Group(), Groups() & Groupdict()

group():
  • A group() expression returns one or more subgroups of the match.
>>> import re
>>> m = re.match(r'(\w+)@(\w+)\.(\w+)','username@hackerrank.com')
>>> m.group(0)       # The entire match 
'username@hackerrank.com'
>>> m.group(1)       # The first parenthesized subgroup.
'username'
>>> m.group(2)       # The second parenthesized subgroup.
'hackerrank'
>>> m.group(3)       # The third parenthesized subgroup.
'com'
>>> m.group(1,2,3)   # Multiple arguments give us a tuple.
('username', 'hackerrank', 'com')

HackerRank Python Solution - Regex and Parsing - Re.start() & Re.end()

start() & end():
  • These expressions return the indices of the start and end of the substring matched by the group.
>>> import re
>>> m = re.search(r'\d+','1234')
>>> m.end()
4
>>> m.start()
0

HackerRank Python Solution - Regex and Parsing - Re.findall() & Re.finditer()

re.findall():
  • The expression re.findall() returns all the non-overlapping matches of patterns in a string as a list of strings.
>>> import re
>>> re.findall(r'\w','http://www.hackerrank.com/')
['h', 't', 't', 'p', 'w', 'w', 'w', 'h', 'a', 'c', 'k', 'e', 'r', 'r', 'a', 'n', 'k', 'c', 'o', 'm']

CodeChef - Python Practice Solution - Mahasena

Problem:
  • Kattapa, as you all know was one of the greatest warriors of his time. The kingdom of Maahishmati had never lost a battle under him (as army chief), and the reason for that was their mighty army, also called as Mahasena.
  • Kattapa was known to be a very superstitious person. He believed that a soldier is "lucky" if the soldier is holding an even number of weapons, and "unlucky" otherwise. He considered the army as "READY FOR BATTLE" if the count of "lucky" soldiers is strictly greater than the count of "unlucky" soldiers, and "NOT READY" otherwise.

HackerRank SQL Solution - Advanced Select - The PADS

Generate the following two result sets:
  • Query an alphabetically ordered list of all names in OCCUPATIONS, immediately followed by the first letter of each profession as a parenthetical (i.e.: enclosed in parentheses). For example: AnActorName(A), ADoctorName(D), AProfessorName(P), and ASingerName(S).
  • Query the number of occurrences of each occupation in OCCUPATIONS. Sort the occurrences in ascending order, and output them in the following format:
There are a total of [occupation_count] [occupation]s.
  • where [occupation_count] is the number of occurrences of occupation in OCCUPATIONS and [occupation] is the lowercase occupation name. If more than one Occupation has the same [occupation_count], they should be ordered alphabetically.

You might also like

Deploy your Django web app to Azure Web App using App Service - F1 free plan

In this post, we will look at how we can deploy our Django app using the Microsoft Azure app service - a free plan. You need an Azure accoun...