Skip to content

Instantly share code, notes, and snippets.

View Josephchinedu's full-sized avatar
🔥
building

Devjoseph Josephchinedu

🔥
building
View GitHub Profile
@Josephchinedu
Josephchinedu / app.py
Created April 6, 2022 09:43
town judge
# The rules:
#The town judge trusts nobody.
# Everybody (except for the town judge) trusts the town judge.
## let's look at the example:
# we've 3 people, and there trust is been displayed like this. [[1,2], [2,3]].
# the trust simply means that 1 trust 2, and 2 trust 3.
# so 3 doesn't trust anyone that makes 3 the town judge
#### steps to solve it.
@Josephchinedu
Josephchinedu / app.py
Created April 9, 2022 12:57
baseball game
# You are keeping score for a baseball game with strange rules. The game consists of several rounds, where the scores of past rounds may affect future rounds' scores.
# At the beginning of the game, you start with an empty record. You are given a list of strings ops, where ops[i] is the ith operation you must apply to the record and is one of the following:
# An integer x - Record a new score of x.
# "+" - Record a new score that is the sum of the previous two scores. It is guaranteed there will always be two previous scores.
# "D" - Record a new score that is double the previous score. It is guaranteed there will always be a previous score.
# "C" - Invalidate the previous score, removing it from the record. It is guaranteed there will always be a previous score.
@Josephchinedu
Josephchinedu / app.py
Created April 9, 2022 15:15
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.
### steps:
# Start with opening parentheses, the number of opening parentheses should equal closing parentheses
# we check if the closing parentheses matches the opening parentheses, then we remove it from consideration list
@Josephchinedu
Josephchinedu / bubble.py
Created April 12, 2022 22:06
sorting algorithm
## bubble sort is the most basic sorting algorithm and it works by repeatedly
## swapping adjcent elements if they're not ordered then it order it
## bubble sort algorithm takes unsorted lisr in pairs and compare each element and swap the highest to the right
## if they are not ordered.
## example:
## def bubbleFunc(data):
## ## this function takes unsorted list
Given an array, rotate the array to the right by k steps, where k is non-negative.
example, we're give an array [1,2,3,4,5,6,7] and we want to rotate it by 3 times
nums = [1,2,3,4,5,6,7]
k = 3
we're going to modulo the k element, just incase the k element is more than the length of our array.
k = k % len(array)
Given an array of integers, write a function to move all "0" to the end od the array or list while maintaing the
relative orde of the elements.
Explanation:
move the non zero elements to the beginning of the array and keep track of their count, initialize a variable for it call, e.g "j".
then, after that set the elements starting from "j" till the end of the array to "0".
Steps:
a. define a pointer "j" at the beginning of the array.
@Josephchinedu
Josephchinedu / rescueboat.txt
Last active May 13, 2022 09:20
rescueboat.txt
Boat to save people.
We're trying to add people in safety boats.
We're given an array of people and an integer limit.
People array is an array of people's weights, so the "i-th" person weights a person at position "i" and each boat can carry at most
the number of limit.
Each boat carries at most 2 people at the same time, given that their weight sum is at most the limit.
Our task is just to return the minimum number of boats to carry a given person.
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.
Note: You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2.
The number of elements initialized in nums1 and nums2 are m and n respectively.
Steps:
1. create a variable that will hold the new value of nums1.
Because, we're selecting the numbers in nums1 and leaving out the zeroes.
"m" is always the length of numbers in nums1.

Given an array of integers arr, return true if and only if it is a valid mountain array. Recall that arr is a mountain array if and only if:

  • arr.length >= 3
  • There exists some i with 0 < i < arr.length - 1 such that:
    • arr[0] < arr[1] < ... < arr[i - 1] < arr[i]
    • arr[i] > arr[i + 1] > ... > arr[arr.length - 1]

Steps:

  • Check if the the input of the arrray is less than 3. if so, return False
@Josephchinedu
Josephchinedu / main.go
Created August 7, 2022 21:59
reverse string in golang
package main
import "fmt"
func main() {
input := "Joseph Chinedu Ogbu"
rev := Reverse(input)
doubleRev := Reverse(rev)
fmt.Printf("original: %q\n", input)
fmt.Printf("reversed: %q\n", rev)