Skip to main content

Posts

Showing posts from January, 2022

One Path to Bankruptcy for Replit

User trying to import a module that's not installed. Instead of bumping him and telling her to use a different workspace on which the module *is* installed, use compute resources to try and install.. nuts.. Starting with : from transformers import TFAutoModelForSeq2SeqLM, AutoTokenizer import gradio as gr model = TFAutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-small") tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-small") def gen_text(input_string, max_length):     inputs = tokenizer(input_string, return_tensors="pt")     outputs = model.generate(**inputs, max_length=max_length)     final_text = tokenizer.batch_decode(outputs[0], skip_special_tokens=True)     return (final_text) demo = gr.Interface(                                                          fn=gen_text,     ...

Prime Factors of a Number in Python

Stub : 63 --> '(3**2)(7)'  You get the idea. The return needs to be '(p1**n1)(p2**n2)..(pk**nk)' How? I admit my first shot was crappy - maintain a dictionary of powers. Then I saw the "best" solution :  def primeFactors(n): ret = '' for i in xrange(2, n + 1): num = 0 while(n % i == 0): num += 1 n /= i if num > 0: ret += '({}{})'.format(i, '**%d' % num if num > 1 else '') if n == 1: return ret Hokay, what's going on here? Really cute way of building up the string as you go along. But, he looking at all the numbers from 2 to n. Do you need to? The even numbers? :) Once you've extracted 2, you'll never need to consider an even number again. Then, to what extend to you need to go? Do you need to go past sqrt(n)? Take 21. You get 3 out of it and stop at 5 because that's close to sqrt(21). What are you left with after yo...

On the Electrodynamics of Generating Hamming Numbers Efficiently

Ok, this was part of my daily drill - something I wanted to spend about 15 minutes on each day. This one ended up eating up two entire day and disrupted my sleep too.. Shows how far behind the curve I am :) Finding it so difficult (when I was pursuing the wrong approach) forced me to ask - why is this one only 4 kyu? (A unit of credit on Codewars). Turns out, if you know the right way, it's easy. The ask :  Write a function that computes the  n th smallest Hamming number. Specifically: The first smallest Hamming number is 1 = 2 0 3 0 5 0 The second smallest Hamming number is 2 = 2 1 3 0 5 0 The third smallest Hamming number is 3 = 2 0 3 1 5 0 The fourth smallest Hamming number is 4 = 2 2 3 0 5 0 The fifth smallest Hamming number is 5 = 2 0 3 0 5 1 The 20 smallest Hamming numbers are given in example test fixture. Your code should be able to compute all of the smallest 5,000 (Clojure: 2000, NASM: 13282) Hamming numbers without timing out. asdf Sound easy? It looks so simple and...

Popular posts from this blog

Align an Embedded Image in Jupyter Markdown

Nice thing is that you don't have to depend on the image existing as a separate file that you can refer to. You can embed it like an image in an email - you get the idea. Jupyter takes care of this for you in the .ipynb file. But, by default, the image is aligned center and is default size. What if you want to set the size? If it were an external file, then you can just resort to standard HTML. But, you want a fully self contained notebook. So? In one cell, above this one, NOT markdown, but code, have an HTML magic where you specify CSS that applies to this TAG. In the cell of interest, where you insert the image after doing Edit > Insert Image, change the "alt text" inside the [] to something the CSS style can refer to and you're done So, (1) looks like : %%html <style>     img[alt=bad_pie]{         float : left;     } </style> And, the cell with the image, when in edit mode, will look like : ![bad_pie](attachment:Capture.PNG) Than...

openCV : Really Filtering by Color

The free openCV crash course : img_NZ_bgr = cv.imread('New_Zealand_Lake.jpg', cv.IMREAD_COLOR) b,g,r = cv.split(img_NZ_bgr) plt.figure(figsize=[20,5]) plt.subplot(141);plt.imshow(r, cmap='gray');plt.title("Red") plt.subplot(142);plt.imshow(b, cmap='gray');plt.title("Blue") plt.subplot(143);plt.imshow(g, cmap='gray');plt.title("Green") # merging imgMerged = cv.merge((b,g,r)) # original code : b,g,r plt.subplot(144);plt.imshow(imgMerged[...,::-1]);plt.title("Merged") Gives you : Coolie McVoolie. But, wait a minute! Are you really going to fall for that? Remember those "3D" glasses you got in magazines as a kid that let you see the page in 3D by using filters (each eye sees the picture from the required angle)? Meaning, if you're looking at the Red channel, you want to see : This! Right? How? Easy Make a blank channel (basically using NumPy zeros) Use that blank channel for the filtered channels, ...

One Path to Bankruptcy for Replit

User trying to import a module that's not installed. Instead of bumping him and telling her to use a different workspace on which the module *is* installed, use compute resources to try and install.. nuts.. Starting with : from transformers import TFAutoModelForSeq2SeqLM, AutoTokenizer import gradio as gr model = TFAutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-small") tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-small") def gen_text(input_string, max_length):     inputs = tokenizer(input_string, return_tensors="pt")     outputs = model.generate(**inputs, max_length=max_length)     final_text = tokenizer.batch_decode(outputs[0], skip_special_tokens=True)     return (final_text) demo = gr.Interface(                                                          fn=gen_text,     ...