Skip to content

Instantly share code, notes, and snippets.

@kkt-ee
kkt-ee / https_server.py
Created December 1, 2023 20:58
https server (replacement of python -m http.server)
""" Three steps to make an https alternative to python -m http.server @kkt
1. shell command to generate the files: certificate.pem and key.pem
$ openssl req -newkey rsa:2048 -nodes -keyout key.pem -x509 -days 365 -out certificate.pem
2. Host the server
3. Add to $PATH
@kkt-ee
kkt-ee / load_mat_file_python.py
Created September 20, 2020 07:57
load a .mat file in python
from scipy.io import loadmat
IMFs = loadmat('IMFData_.mat')
# -----< INSPECTING IMFData_ >
type(IMFs['IMFData_']),IMFs['IMFData_'].shape
IMFData_ = IMFs['IMFData_'] #numpy.ndarray
IMFData_.dtype
IMFData_.shape # IMFData_ : {2Darray, string} x 200 instances
IMFData_[0].shape # 0-199(max)
@kkt-ee
kkt-ee / read_all_csv_files_from_dirx.m
Created September 11, 2020 10:19
MATLAB read all csv files from a directory (eg. dirx)
%%
pathx = '/root/dirx/'; %---o directory path
files = fullfile(pathx,'*.csv');
d = dir(files); clearvars files
%%
files_with_abs_path = {};
file_name_head ={};
for k=1:numel(d) %---o LOOPING through each filenames
@kkt-ee
kkt-ee / update_gentoo_linux_cmds.sh
Created September 9, 2020 10:27
Open a konsole and display commands to update gentoo linux
#!/bin/bash
konsole --noclose -e echo $'Gentoo update copy/paste commands:
$ emaint -a sync
(optional) $ emerge --oneshot sys-apps/portage
$ emerge --ask -uDU --keep-going --with-bdeps=y @world
o emaint -a sync \no running in other terminal...' &
konsole --noclose -e emaint -a sync &
"""
Implement the function unique_in_order which takes as argument a sequence and returns a list of items without any elements with the same value next to each other and preserving the original order of elements.
For example:
unique_in_order('AAAABBBCCDAABBB') == ['A', 'B', 'C', 'D', 'A', 'B']
unique_in_order('ABBCcAD') == ['A', 'B', 'C', 'c', 'A', 'D']
unique_in_order([1,2,2,3,3]) == [1,2,3]
"""
"""
There is a queue for the self-checkout tills at the supermarket. Your task is write a function to calculate the total time required for all the customers to check out!
input
customers: an array of positive integers representing the queue. Each integer represents a customer, and its value is the amount of time they require to check out.
n: a positive integer, the number of checkout tills.
output
The function should return an integer, the total time required.
"""
Count the number of Duplicates
Write a function that will return the count of distinct case-insensitive alphabetic characters and numeric digits that occur more than once in the input string. The input string can be assumed to contain only alphabets (both uppercase and lowercase) and numeric digits.
Example
"abcde" -> 0 # no characters repeats more than once
"aabbcde" -> 2 # 'a' and 'b'
"aabBcde" -> 2 # 'a' occurs twice and 'b' twice (`b` and `B`)
"indivisibility" -> 1 # 'i' occurs six times
"""
Write a function that takes an integer as input, and returns the number of bits that are equal to one in the binary representation of that number. You can guarantee that input is non-negative.
Example: The binary representation of 1234 is 10011010010, so the function should return 5 in this case
"""
def countBits(n):
b = bin(n)[2:]
return sum([int(x) for x in b])
#OR
"""
Given an array of ones and zeroes, convert the equivalent binary value to an integer.
Eg: [0, 0, 0, 1] is treated as 0001 which is the binary representation of 1.
"""
def binary_array_to_number(arr):
# your code
return int(''.join([str(x) for x in arr]),2)
#OR
"""
Number of people in the bus
There is a bus moving in the city, and it takes and drop some people in each bus stop.
You are provided with a list (or array) of integer arrays (or tuples). Each integer array has two items which represent number of people get into bus (The first item) and number of people get off the bus (The second item) in a bus stop.
Your task is to return number of people who are still in the bus after the last bus station (after the last array). Even though it is the last bus stop, the bus is not empty and some people are still in the bus, and they are probably sleeping there :D
Take a look on the test cases.
Please keep in mind that the test cases ensure that the number of people in the bus is always >= 0. So the return integer can't be negative.
The second value in the first integer array is 0, since the bus is empty in the first bus stop.
"""