Skip to content

Commit

Permalink
2024-09-16 17:12:34.795202 new snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardocerqueira committed Sep 16, 2024
1 parent d0ccec8 commit 3794c29
Show file tree
Hide file tree
Showing 16 changed files with 408 additions and 435 deletions.
30 changes: 30 additions & 0 deletions seeker/report.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
--------------------------------------------------------------------------------
2024-09-16 17:12:34.795202
--------------------------------------------------------------------------------
On branch main
Your branch is up to date with 'origin/main'.

Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
deleted: snippet/Wallet.java
deleted: snippet/brute_force_affine.py
deleted: snippet/impress.py
deleted: snippet/main.py
deleted: snippet/remove-taint.sh
deleted: snippet/script.py
deleted: snippet/test_logging.py
deleted: snippet/test_namaster.py
deleted: snippet/testing_logging_levels.py

Untracked files:
(use "git add <file>..." to include in what will be committed)
snippet/base_conversions.py
snippet/generate_signed_urls.py
snippet/ipynb_importParentDir_pyCodeTemplate.py
snippet/main.java
snippet/module_5_2.py
snippet/program.java

no changes added to commit (use "git add" and/or "git commit -a")

--------------------------------------------------------------------------------
2024-09-13 17:12:30.412983
--------------------------------------------------------------------------------
Expand Down
30 changes: 0 additions & 30 deletions seeker/snippet/Wallet.java

This file was deleted.

110 changes: 110 additions & 0 deletions seeker/snippet/base_conversions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#date: 2024-09-16T17:00:30Z
#url: https://api.github.com/gists/dd83ecd985ad1817d72ae92764b4921c
#owner: https://api.github.com/users/Marcus5408

# conversion.py
# -------------
# Description:
# A simple program that converts a number from one base to another using either
# the successive division method or the weighted multiplication method.
# -------------
# Usage:
# In a terminal, run the following command:
# python3 conversion.py <method> <number> <base>
#
# <method> selects the conversion method using one of the following:
# - divide: successive division method
# - multiply: weighted multiplication method
# <number> is the number to convert.
# <base> is the target base (for successive division)
# or the base of the number (for weighted multiplication).
# -------------
# (c) Issac Liu, 2024

from typing import Union, Literal
import sys


def success_div(n, base):
remainder = 0
result = 0
charset = "0123456789"
if base > 10:
if base == 16:
charset = "0123456789ABCDEF"
else:
print(
"You have entered a base greater than 10. Please enter every digit of your base from least to greatest."
)
values = input("")
charset = values if len(values) == base else "0123456789ABCDEF"
if base < 10:
while n != 0 or n > base:
remainder = n % base
quotient = n // base
print(f"{n}/{base} = {quotient}r{remainder}")
result = result * 10 + remainder
n = quotient
# reverse the result
result = int(str(result)[::-1])
print(f"\n{result}")
else:
result = ""
while n != 0:
remainder = n % base
quotient = n // base
if base > 10 and remainder > 9:
hex_value = f" ({remainder} -> {charset[remainder]})"
print(f"{n}/{base} = {quotient}r{remainder}{hex_value}")
else:
print(f"{n}/{base} = {quotient}r{remainder}")
result = charset[remainder] + result
n = quotient
print(f"\n{result}")

return result


def weighted_multiply(n: Union[int, str], base: int) -> int:
if isinstance(n, str):
n = n.upper()
charset = "0123456789ABCDEF"
list = [charset.index(x) for x in n]
else:
list = [int(x) for x in str(n)]

weights = [base**i for i in range(len(list) - 1, -1, -1)]
result = [a * b for a, b in zip(list, weights)]

for i in range(len(result)):
if base > 10 and list[i] > 9:
hex_value = f" ({charset[list[i]]} -> {list[i]})"
print(
f"{list[i]}{hex_value} * {base}^{len(list) - i - 1} = {list[i]} * {weights[i]} = {result[i]}"
)
else:
print(
f"{list[i]} * {base}^{len(list) - i - 1} = {list[i]} * {weights[i]} = {result[i]}"
)

print(f"\n{' + '.join([str(x) for x in result])} = {sum(result)}")
return sum(result)


if __name__ == "__main__":
if len(sys.argv) != 4:
print("Usage: python conversion.py <method> <number> <base>")
sys.exit(1)

method = sys.argv[1]
n = int(sys.argv[2]) if sys.argv[2].isdigit() else sys.argv[2]
base = int(sys.argv[3])

if method == "divide":
success_div(n, base)
elif method == "multiply":
weighted_multiply(n, base)
else:
print(
"Invalid method. Use 1 for division method or 2 for weighted multiply method."
)
27 changes: 0 additions & 27 deletions seeker/snippet/brute_force_affine.py

This file was deleted.

52 changes: 52 additions & 0 deletions seeker/snippet/generate_signed_urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#date: 2024-09-16T16:57:20Z
#url: https://api.github.com/gists/05da91b9b34799ff6fd4254cffba7d3e
#owner: https://api.github.com/users/rlank

from google.cloud import storage
from datetime import timedelta
import os

# Set the path to your service account key file
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = '/path/to/key/json'

def generate_signed_urls(bucket_name, prefix, expiration_time):
"""
Generates signed URLs for files in the given bucket and prefix.
:param bucket_name: Name of the GCS bucket.
:param prefix: Prefix of the files in the GCS bucket.
:param expiration_time: Time in minutes for which the signed URL should be valid.
:return: List of tuples containing the file name and signed URL.
"""
# Initialize the client
# This uses the default credentials. Make sure that the GOOGLE_APPLICATION_CREDENTIALS environment variable is set.
storage_client = storage.Client()

# Get the bucket
bucket = storage_client.bucket(bucket_name)

# Get blobs (files) with the given prefix
blobs = bucket.list_blobs(prefix=prefix)

signed_urls = []
for blob in blobs:
# Generate a signed URL for each blob
url = blob.generate_signed_url(
expiration=expiration_time,
version='v4' # Use V4 signing
)
signed_urls.append((blob.name, url))

return signed_urls
# Usage
bucket_name = 'fuelcast-data'
prefix = 'fuel/rapid-2024-conus/'

# Longest allowable time is one week
exp_time = timedelta(days=7)

signed_urls = generate_signed_urls(bucket_name, prefix, expiration_time=exp_time)

# Print signed URLs
for file_name, url in signed_urls:
print(f"File: {file_name} - Signed URL: {url}")
59 changes: 0 additions & 59 deletions seeker/snippet/impress.py

This file was deleted.

26 changes: 26 additions & 0 deletions seeker/snippet/ipynb_importParentDir_pyCodeTemplate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#date: 2024-09-16T16:51:30Z
#url: https://api.github.com/gists/c4028cf4e3de861d0dda7c7edf552b57
#owner: https://api.github.com/users/birdflyi

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Python 3.7 # Your python version

# @Time : ${DATE} ${TIME}
# @Author : 'Lou Zehua' # Your name
# @File : ${NAME}.py

import os
import sys

if '__file__' not in globals():
# !pip install ipynbname # Remove comment symbols to solve the ModuleNotFoundError
import ipynbname

nb_path = ipynbname.path()
__file__ = str(nb_path)
cur_dir = os.path.dirname(__file__)
pkg_rootdir = os.path.dirname(cur_dir) # Should be the root directory of your project.
if pkg_rootdir not in sys.path: # To resolve the ModuleNotFoundError
sys.path.append(pkg_rootdir)
print('-- Add root directory "{}" to system path.'.format(pkg_rootdir))
36 changes: 36 additions & 0 deletions seeker/snippet/main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//date: 2024-09-16T17:10:28Z
//url: https://api.github.com/gists/12ac859a0f6d500e52d8ae7999e2b395
//owner: https://api.github.com/users/qren0neu

class Solution {
public long maxScore(int[] nums1, int[] nums2, int k) {
// if use priority queue, we can have:
// 1. when we poll in the queue, we remove the min
// so the sum of nums1 should be larger
// but, we have to calculate the minimum dynamically in nums2
// if we can combine nums1 and nums2 somehow together, we can solve the problem
int[][] arr = new int[nums1.length][2];
for (int i = 0; i < nums1.length; i++) {
arr[i][0] = nums1[i];
arr[i][1] = nums2[i];
}
Arrays.sort(arr, (int[] arr1, int[] arr2) -> arr2[1] - arr1[1]);
PriorityQueue<Integer> pq = new PriorityQueue<Integer>(k, (a,b) -> a - b);
long score = 0;
long sum = 0;
for (int[] pair : arr) {
// pair: nums1, nums2
int min = pair[1];
pq.offer(pair[0]);
sum += pair[0];
if (pq.size() > k) {
int removed = pq.poll();
sum -= removed;
}
if (pq.size() == k) {
score = Math.max(score, sum * min);
}
}
return score;
}
}
Loading

0 comments on commit 3794c29

Please sign in to comment.