#algos-and-data-structs
1 messages · Page 99 of 1
👍
Hello guys! I have a question regarding DFS/BFS.
I understand that DFS is backtracking/ exploring, and BFS is finding a pathway. But how do i determine when to use either one?
is anyone available to help with a for loop problem please ping me
does anybody
know
how to implement karatsuba algorithm in python
switching
from cpp to python
interesting. why implement it?
CPython uses karatsuba for very large numbers
is there anyway to make this code faster: ```py
import sys
raw_input = sys.stdin.readline
m = int(raw_input())
n = int(raw_input())
brushes = raw_input()
stroke = []
colors = [['B' for _ in range(n)] for _ in range(m)]
for i in range(int(brushes)):
g = raw_input().split(' ')
if stroke.count(g) == 1:
stroke.remove(g)
else:
stroke.append(g)
def changeColumn(num,colors,index):
if num == 0:
return colors
colors[num-1][index] = 'G' if colors[num-1][index] == 'B' else 'B'
num -= 1
changeColumn(num,colors,index)
def countGold(c,options,i):
if options == []:
s = 0
for l in c:
s += l.count("G")
print(s)
return
area = options[i][0]
times = int(options[i][1]) - 1
if area == "R":
c[times] = list(''.join(c[times]).replace("G","A").replace("B","G").replace("A","B"))
elif area == "C":
changeColumn(m,c,times)
options.remove(options[i])
countGold(c,options,i)
countGold(colors,stroke,0)
is it too slow?
@kindred coyote changeColumn is recursive, which is unusual. you can use a loop.
also, it returns a value, but nothing seems to use it?
Actually I found the problem. It's generating colors. A program will need to generate a 10^6 x 10^6 array in less than 2-3 seconds
I need to solve this problem without generating an array
as that takes up too much time
is there a good resource to learn syntax of python linked list? I understand the concept theoretically that the current node points to memory address of the next node and so on. But I find it a bit confusing to implement it. And every other website I go to has a different way of implementing it.
@dapper sapphire it is pretty simple.
in essence, you have a head node, and a tail node.
let us say you have these 5 values that you wish to initiate with the Linked List:
1, 2, 3, 4, 5
and you have a head field, and a tail field in your class.
the tail represents the last node in the Linked List, while the head represents the first.
do you follow so far?
he wanted an answer for exams and its wright because for each time in n you will move length -1 which means you will move length *n
Yeah, I follow it so far, the tail would point to None and head would point to the first element.
nope, the tail actually points to the first element once it is initialized.
so normally when i implement my linked lists, i pop(0) the first element off the list of initialized values, and assign that to the head, and the tail to that as well.
once you do that, you begin to "chain" values.
def __init__(self, *entries):
entries = list(entries)
self.head = Node(entries.pop(0))
self.tail = self.head
so when you "chain" values, you loop through the remaining values to initialize, and for each element, you do the following:
create a new node. we will call it
nfor now.
set thenext_nodefield of the current tail ton
set the tail ton
this is so the cycle can repeat for the next node you add.
@dapper sapphire let me know if you want me to rephrase anything, or you do not understand it.
wait can we do *enteries? I thought we cant use a pointer in Python. and would having a pointer or not having a pointer make a difference in this case?
*entries defines a varadic argument called entries
the * does not mean "pointer to type entries" like it does in C.
it just means "this is a varadic argument."
this is so you could do this, and have it be valid syntax:
my_list = LinkedList(1, 2, 3, 4, 5)
instead of
my_list = LinkedList([1, 2, 3, 4, 5])
you following with that?
oh right ok I get it so with variadic argument we dont have to worry about specifying the number of parameters.
on line 3 you pop the 0th element and assign the value to head. On line 4 you assign the popped value also to the tail.
yup!
let me know if you need help with any other datastructures, or you want to clear some things up.
i would be glad to help.
wait is that it?? That's all for linked list?

yeah pretty much.
that is all it takes to initialize one.
there are other operations, and you also need to setup iteration, i can help you with that if you would like.
but yeah, that is basically all it takes to create one. Linked Lists are extremely easy to initialize.
So what you just did with 4 lines of code to initialize a linked list, is this similar to the below code where we create a Node class and a LinkedList class:
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
yeah that is pretty similar to how i would write it.
i mean i could show you how to setup a Linked List in C as well if you wanted me to, but this is a Python server.. the option is always there, though!
assuming you do not know.
I think C would just confuse me even though I have programmed in it before. Python would be fine. But hey when you do:
self.head = Node(entries.pop(0))
self.tail = self.head
You are not pointing anything to None? both head and tail point to the 0th element.
yeah, that is intentional.
it is because if we start looping through with the tail as None, we would have no way to add something to the end of the list without using the head field.
we do not want to reassign the head field during initialization.
what form of adding? insertion, or appending?
appending
oh, appending is easy. you do the exact same thing you did during initialization with the tail field.
except it is in it's own function.
are you creating function based linked list?
well of course. classes have methods bound to them.
I mean I guess, we could create a linked list either way.
or by "function-based" did you mean in a way similar to how C operates on datastructures?
so suppose the code you wrote was in a class called Node, then would we create a add method like below:
def add(self, data):
self.head = Node(data)
using functions in a file that accepts a struct or something along those lines as an argument?
that seems more like a prepend method, but yeah, close.
you are missing an important factor, though.
that will essentially unlink the entire Linked List, since you are not assigning the next field of the new node to the previous head.
so make sure you do that.
def add(self, data):
temp = Node(data)
temp.next = self.head
self.head = temp
So something like this.
bingo
whenever you add or remove any Nodes into your Linked List, make sure you check you are not preventing access to other parts of the list.
just a general word of advice, really.
How did you get comfortable with linked lists? Just practice?
more or less, yeah
i am familiar with a few more datastructures.
Hashtables, Queues, Stacks, most types of Trees, Sets, and Arrays.
those are the ones i am aware of how to create.
Queues and Stacks are essentially just Linked Lists or Arrays with operations that only operate on the bottom or top.
Yeah I was doing Queues and Stacks yesterday and I found those pretty easy to understand and implement.
When you say you implemented a hashtable, you mean, you created a dictionary?
yeah, Hashtables are an implementation of a Dictionary, also known as Associative Arrays.
Hashtables are next on my list of things to implement. Because I use dictionaries a lot on different projects and I find it really powerful. Hey man, really appreciate all the help!!
anytime. feel free to ask me for help on implementing them Hashtables. i would be glad to help out.
I am glad that I scrolled down to the server and posted the question here. Yeah of course, much appreciated!
Lolll
get it at the base case condition of the recursive function
are array lists better than single linked list when you want to store a very big data base?
import sys
raw_input = sys.stdin.readline
m = int(raw_input())
n = int(raw_input())
brushes = int(raw_input())
listed_rows = []
listed_columns = []
gold = 0
duplicates = []
for i in range(brushes):
g = raw_input().split()
g[1] = int(g[1])
if g in duplicates:
continue
else:
duplicates.append(g)
if g[0] == "R":
gold += (n - len(listed_columns))
gold -= len(listed_columns)
listed_rows.append(g[1])
if g[0] == "C":
gold += (m - len(listed_rows))
gold -= len(listed_rows)
listed_columns.append(g[1]-1)
print(gold)
``` does any1 know how to make this even faster? I still get a time limit exceeded error
(Time Limit is 4 seconds)
what are you trying to do tho?
lemme send problem
A new and upcoming artist has a unique way to create checkered patterns. The idea is to
use an M-by-N canvas which is initially entirely black. Then the artist repeatedly chooses
a row or column and runs their magic brush along the row or column. The brush changes
the colour of each cell in the row or column from black to gold or gold to black.
Given the artist’s choices, your job is to determine how much gold appears in the pattern
determined by these choices.
The first line of input will be a positive integer M. The second line of input will be a positive
integer N. The third line of input will be a positive integer K. The remaining input will be
K lines giving the choices made by the artist. Each of these lines will either be R followed
by a single space and then an integer which is a row number, or C followed by a single space
and then an integer which is a column number. Rows are numbered top down from 1 to M.
Columns are numbered left to right from 1 to N.
and u output how many gold squares there r
I also have this code: ```py
for i in range(int(brushes)):
g = raw_input().split(' ')
if stroke.count(g) == 1:
stroke.remove(g)
else:
stroke.append(g)
def changeColumn(num,colors,index):
if num == 0:
return colors
colors[num-1][index] = 'G' if colors[num-1][index] == 'B' else 'B'
num -= 1
changeColumn(num,colors,index)
def countGold(c,options,i):
if options == []:
f = 0
for l in c:
f += l.count("G")
print(f)
return
area = options[i][0]
times = int(options[i][1]) - 1
if area == "R":
c[times] = list(''.join(c[times]).replace("G","A").replace("B","G").replace("A","B"))
else:
changeColumn(m,c,times)
options.remove(options[i])
countGold(c,options,i)
countGold(colors,stroke,0)
are you sure you need to iterate through the entire list?
It's basically saying this: ```
Enter a number M
The next M lines, enter....
so in the 1st code, the for loop is a must unless there's another way to do that
can anyone help me converting this bash to python
#!/bin/bash
packageInfo.bash
Purpose: Generates a report to displaying specified information of installed software
USAGE: ./packageInfo.bash [application-name]
Author: *** INSERT YOUR NAME ***
Date: *** CURRENT DATE ***
if [ $(whoami) != "root" ] # only runs if root
then
echo "You must be logged in as root." >&2
exit 1
fi
if [ $# -ne 1 ]
then
echo "Your command must have a application-name as argument" >&2
echo "USAGE: $0 [application-name]" >&2
exit 1
fi
Create report title (echo with -e option allows newline \n character to be used)
echo -e "\nSOFTWARE PACKAGE INFORMATION REPORT" > /root/package-info.txt
echo -e "Date: $(date +'%A %B %d, %Y (%H:%M:%p)')\n\n " >> /root/package-info.txt
Clear screen and use Here Document to display select on report items to read into variable
clear
cat <<+
Available Package Information Items:
Name
Summary
Version
License
Source
URL
+
read -p "Enter word(s) shown above separated by spaces: " choice
Convert spaces to pipe symbol (|)
processedChoice=$(echo $choice | sed 's/ /|/g')
Use sed with extended regular expressions to only print those matching report elements
rpm -qi $1 | sed -r -n "/^($processedChoice)/ p" >> /root/package-info.txt
cat <<+
File "/root/package-info.txt" has been created
+
Save, set permissions, and then run that shell script for the application gedit. Did it create that report? Try running the script without an argument - What did it do?
Use the wget command to download, study, and run the following shell scripts on-line:
What is an algorithm? My understanding of writing algorithms is figuring out what exactly to do with code to find the results you want. Is this an accurate definition?
@kindred coyote can you send a link to this problem, I want to test a solution I think i have using ||sets||
same
ur right. solution is this: ```py
import sys
rw = sys.stdin.readline
M = int(rw())
N = int(rw())
choices = set()
K = int(rw())
for i in range(K):
g = input()
if g in choices:
choices.remove(g)
else:
choices.add(g)
numR = 0
numC = 0
s = 0
for choice in choices:
if choice[0] == "R":
numR += 1
elif choice[0] == "C":
numC += 1
for choice in choices:
if choice[0] == "R":
# numR += 1
s += N - numC
elif choice[0] == "C":
# numC += 1
s += M - numR
print(s)
moral of the story: searching lists r more expensive than searching sets when it comes to bigger numbers
what I was planning on was have a set of row numbers and column numbers, then for each input, add or remove that number from the appropriate set depending on if it exists or not (because if the same row is chosen twice it cancels out) then at the end, the number of gold tiles will be M * len(columns) + N * len(rows) - 2 * len(columns) * len(rows) i think, because thats the number of tiles that are gold because of the rows + the numbre of tiles made gold because of the columns, minus the overlap
so that would be O(K) time and O(N + M) space
i think thats similar to what you did but the only real difference being that final for-loop to calculate s you may be able yo make O(1) using M * numC + N * numR - 2 * numC * numR
Really don't get what you mean. Did you intend to reply to that message specifically (me responding to someone asking for help with an essay)?
@dreamy arrow And if you meant my earlier analysis of that code snippet's complexity - no, it's n!, as the plot proves.
we have this question for school but none of us can find the logic error, we've been staring at it for half an hour.
the only problem I see is with the loop
but it's less a logic error, and more like an undeclared name
because, well, response is not set anywhere
I think they mean it as some sort of snippet from a larger program
so it is probably declared somewhere else
That's true, but it's also not changed over the course of the loop
So that loop will either not execute at all, if response is not "Y", or it will loop forever if it is.
There's no code that allows exiting the loop via X as it states.
ok, thanks for the help!
I found the answer, every time you enter x to exit the loop it adds 1 to rCount anyway so rCount ends up wrong.
lol, so you're not supposed to notice that there's also no input being taken?
input is being taken though right?
Is this a snippet from an existing language or pseudocode?
pseudocode
Okay thanks
reverse a doubly linked list 😔
i refuse to believe i just heard someone say "array list"
@mystic basin i mean yeah i guess.
what's wrong with that lol
it's a class in java lol
like python lists
yeah
i thought it was another case of Java just being special, like with Hashtables and Hashmaps being subtly different things (not sure if that is still true)
yeah, they're different, but hashtable isn't really used anymore
it does not, but as far as i know modifying something as you loop through it is not a good idea.
but maybe it does not involve that, not sure. never tried it.
you could potentially just construct a new Linked List, though?
that is my first thought.
yeah, but i think the challenge is doing it in place
mostly yeah
so does anyone have any good resources to start learning these
or books
i'm not doing a bootcamp to learn how to do interview questions so anything would be helpful
i do not have any books, but i started with a Linked List, so maybe do some Googling and find out how to implement one.
i could quickly explain right now, though.
Linked Lists are a pretty important datastructure to understand in order to implement some other datastructures.
at least, the idea of "linking" Nodes is.
all i know is the meme about reversing a linked list
so i highly suggest you start there.
i remember a Crash Course Computer Science video introduced me into Linked Lists and it did a pretty good job of explaining them.
yeah, probably.
i want to create at least one or two portfolio things before i go ham over hackerrank and leetcode
especially if you want to go into interviews for it.
ok great thanks
can i bring the hackerrank and leetcode questions here too or should i only ask in help channels
i mean i do not see anything wrong with asking here.
just do not ask questions about Euler challenges i believe.
ok great
you can get in trouble for posting answers to those i think.
not sure if the staff here enforce that, or not.
pls i can't even do list comprehension bc i've never seen it before
that might be after the one hundredth question, though.
that doesn't have much to do w DSA
List Comprehension is just when you construct a new list based off another iterator.
i can google list comprehension questions and try and solve those
x = [n for n in range(10)]
puts all the numbers from zero to nine in the list.
i don't get what n for n is tho
it is equivalent to this:
x = []
for n in range(10):
x.append(n)
n for n is just syntax
yeah, it might be better to represent comprehension like this:
n = [(expression) (iterator)]
x = [n + 5 for n in range(10)]
this will put every number zero to nine, add five to it, then put it into the list.
the for loop bit is essentially just where you get your variable from, and the expression describes how that variable should be mutated before being put into the list.
in this case, we add five to it.
but this is off-topic to the channel, so i guess i will stop here.
it is no problem
thank you
anytime
Is anyone free to just look over an algo solution I created? I got it to run and I think pretty effiecntly but I'm at the point where I want to be able to see how to improve my method of code.
def load(wordList):
trie = {}
for word in wordList:
addWordToTrie(trie, word, 0)
return trie
def addWordToTrie(d, word, idx):
if idx >= len(word):
d['leaf']=True
return
if word[idx] not in d:
d[word[idx]] = {'leaf':False}
addWordToTrie(d[word[idx]], word, idx+1)
def findWords(trie, word, currentWord):
myWords = set()
for letter in word:
if letter in trie:
newWord = currentWord + letter
if trie[letter]['leaf']:
myWords.add(newWord)
myWords = myWords.union(findWords(trie[letter], word, newWord))
return myWords
def main(keypads, wordlist):
results = []
minLength = 5
trie = load(wordlist)
for pad in keypads:
count = 0
for word in sorted(findWords(trie, pad, "")):
if len(word) >= minLength:
count = count+1
results.append(count)
return results
wordlist= ['APPLE', 'PLEAS', 'PLEASE']
keypads = ['AELWXYZ','AELPXYZ','AELPSXY','SAELPRT','XAEBSKY']
print(main(keypads, wordlist))
You're attempting to solve a puzzle in an escape room with your team.
You need to open a door to get to the next stage.
There are several doors, each with a different keypad on it.
The keypads each have 7 keys, containing 7 distinct letters.
The instructions state that one of the keypads will open the correct door leading to the next stage.
Your job is to find a word that unlocks the correct keypad.
After struggling for some time, the escape room leader gives a clue.
The first letter of the keypad is guaranteed to be in the word that opens the door.
We will call this letter the "key" letter.
The goal of this exercise is to come up with as many words possible that your team can test out on the keypads and find the correct combination to go the next stage of the game.
WHAT YOU KNOW:
The correct combination will be a valid english word.
The words are at least 5 letters long, with no maximum length.
The "key" letter will be in the correct answer.
The words do not contain any letters outside the seven letters on the keypad.
Letters may be reused, inlcuding the "key" letter.
For our purposes, we'll express each set of keypad letters as a string of length 7, where the first letter (the one at posistion 0), is the key letter.
An array of integers. Each Integer should be the number of valid words in the correspdoning lock.
REQUIREMENTS:
Implement a function numKeypadSolutions(wordlist, keypads) that follows the function signature below:
INPUT:
wordlist: An array of strings. This is your list of "valid English words" for the purposes of the following puzzles.
keypads: An array of strings. Each string is the sequence of letters on the keypad.
OUTPUT:
An array of integers. Each integer should be the number of valid words in the corresponding lock.
CONSTRAINTS:
Both the wordlist and the keypad letters will be supplied in all capital letters.
All words in the wordlist will be of length 5 or greater.
Every sequnece of keypad letters will be of exactly length 7.
Every sequence of keypad letters will consist of 7 distinct letters.
EXAMPLE:
INPUT:
wordlist: ['APPLE', 'PLEAS', 'PLEASE']
keypads: ['AELWXYZ', 'AELPXYZ', 'AELPSXY', 'SAELPRT', 'XAEBKSY']
expected output: [0,1,3,2,0]
EXPLANATION:
None of the words in the wordlist can be formed from the letters in keypad 0.
Only APPLE is valid for keypad 1.
APPLE, PLEAS, and PLEASE are valid for keypad 2.
Only PLEAS, and PLEASE are valid for keypad 3, since APPLE does not contain the key letter "S".
None of the words are valid for keypad 4, since none contain the key letter "X".
- I realize my solution doesn't apply the "key" letter though so that's what I'm attempting to throw in right now but it keeps messing everything up. *
- The expected output should be [0,1,3,2,0] and I get [0,1,3,3,0] since I haven't been able to throw in the if check properly for the index of keypad I think
One thing, is that you can break the test if the tested letter fails, this cuts down the time complexity
hi
good afternoon
result = [0]*len(keypads)
for word in wordlist:
for keypad in keypads:
for letter in word:
add = False
if letter not in keypad:
break
else:
add = True
if add == True:
result[keypads.index(keypad)] +=1
print(result) @drowsy minnow
@drowsy minnow there's an error in the expected output
I believe it should be 3 just as you yielded
I posted an alternative without new dicts and stuff
Thanks I will check this out!
This is just within the main function correct? I
ah yeah okay I see...
you can cut the word length a bit by making a set, but I doubt that operation is faster than just running one extra round
if you have mega long words it's better to make them in to sets
https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html
If I'm reading this right, it is a vector - it's different from Vector by lack of synchronisation, so bad things can happen if you try accessing it from several threads.
though I didn't know Vector was synchronized, so it's more like ArrayList is the normal vector-like thingie in Java
can binary trees contain duplicate elements?
can't tell if they're supposed to be unique or not
@spiral scaffold of course they can
if by elements you mean Nodes containing the same value
no problem.
Hello there, I'm trying to make a program that defines the ID in a string and process it. I already made it with a full string (I made a censor algorithm which defines the word that I defined and changes those words into "censored"). But I can't make it with an integer in a string. I can't convert the string into integer in a for loop. Can you help me please?
what exactly do you want to do
find big numbers and remove them?
use regex?
I'm a newbie programmer and I don't know exactly what that is.
!e
import re
print(re.sub(r'\d{10,}', '<replaced>', 'abc def 123 456 1234567890123 ghi jklmnopqr'))
@brave oak :white_check_mark: Your eval job has completed with return code 0.
abc def 123 456 <replaced> ghi jklmnopqr
I'll give it a look
something like that, yeah
Alright, thanks a lot ^^
So I hvae an assignment from my teacher to add up quarters, nickels, pennies, etc and get the dollar and cent amount, and then get the dollar and cent amount separately from each other
would I just use the split function and split on the decimal? He doesn't say at all how to do it
or is there like another way to do it
you'd be adding numbers, right
yes
why would you have a string to split
because it would end up to be like $2.38
I need to get the dollar and cent separated
no
unless im just tweaking and not understanding this
think about it
this way
the various coins
are multiples
of a cent
you add them all up
you get a final number.
that represents the number of cents
in the result
THEN you can convert it into other forms
one of which is the dollar and cent amount with a dollar sign
(string)
or just get the number of dollars in that amount
(int)
addOutput = (quarters * 0.25) + (dimes * 0.10) + (nickels * 0.05) + (pennies * 0.01)
This is what I have
so far
It gives me 2.05 with a certain input
this would give you
the number of dollars
with cents as the fractional part
I would suggest
you store the number of cents
as an int instead
how to create a data structure and a alogrithm
download the latest firmware for your organic neural net
Hi everyone soleone know what's the condition in a* Algorithm when f(n) is duplicated!!?
Hey 🙂 I'm not sure I understand what you mean. Could you elaborate?
Is there an easy way to get the total numbers of unique items over multiple lists? Say I have a list {h, y, e, w} and a list {a, c, h, e} the total number of unique elements would be 6 while the total elements would be 8. The problem is I'm doing this with about 4k items over a couple hundred different lists so I need an efficient way of doing it.
4k items total
just throwing them into a set should be fast enough
algorithms?
I'm struggling with coming up for a cross product between two n-dimensional vectors.
I only have 2-d and 3-d
def cross_product(a,b):
if a==b or isinstance(a,int)or isinstance(b,int):return(0,0,0)
if not(hasattr(a,'__iter__')and hasattr(b,'__iter__'))or len(a)!=len(b)or not len(a):return NotImplemented
if len(a)==2:return(0,0,((a[0]**2+a[1]**2)**0.5)*((b[0]**2+b[1]**2)**0.5))
elif len(a)==3:
"""| i j k |
| a0 a1 a2|
| b0 b1 b2|"""
return(
a[1]*b[2]-a[2]*b[1],
b[0]*a[2]-a[0]*b[2],
a[0]*b[1]-a[1]*b[0]
)
else:return NotImplemented
is there even such thing as a 4-dimensional cross product?
You can can any arbitrary number of dimensions. That is what makes linear algebra so unintuitive.
which direction would the cross product point in in 4 dimensions, then?
what is a cross product in higher dimensions?
no idea tbh
Hey guysI have a list of dicts that is generated with this:
today = datetime.combine(date.today(), time())
data_list = []
time = today
while True:
if time.timestamp() < datetime.now().timestamp():
data_list.append({int(time.timestamp()): {time:time.strftime('%I%p'), 'success': 0, 'fail': 0}})
time += timedelta(hours=3)
else:
break
While I iterate through data_list is there a way I can get the key of each item in the list?
This is how the data list looks
[{1614031200: {'time': '12AM', 'success': 0, 'fail': 0}}, {1614042000: {'time': '03AM', 'success': 0, 'fail': 0}}, {1614052800: {'time': '06AM', 'success': 0, 'fail': 0}}, {1614063600: {'time': '09AM', 'success': 0, 'fail': 0}}, {1614074400: {'time': '12PM', 'success': 0, 'fail': 0}}]
Oh never mind I found it:
keys = all_keys = set().union(*(d.keys() for d in data_list))
👋 Are there any ways I could append values from a current predefined Pandas dataframe to a new column in a new created dataframe?
when in 3D you do a cross product of 3 (or more i think) vecs you obtain a 4D vector, then in 4D if you do a cross product of some vecs you obtain a 5D vec, and so on
it always goes up by one dimension (given that you have enough vectors occuping all the dimensions i suppose)
So I just got this curve which is the result of a genetic algorithm simulation depending on the selection rate for the TSP : https://media.discordapp.net/attachments/780135902524997642/813810960140992512/SR_0.9_to_1.png
I find really weird what happen near 100% selection rate, is that supposed to happen?
Or do you think it's related to my program?
this was a zoom I made with 10 different selection value btw 0.9 and 1, here is the global curve (with 10 points again) https://cdn.discordapp.com/attachments/780135902524997642/813809866895523911/selection_rate_graph.png
I can't see any reason why 100% selection rate get way better results than 98%
@hard bane I believe cross product is only defined for 3d, actually - it's a pretty weird operation in that regard.
In exterior algebra terms, [a,b] is defined as *(a ^ b), where ^ is the exterior product and * the Hodge star operator. Now, * maps k-forms to n-k-forms, and for vectors (a vector is a 1-form) a,b, a ^ b is a 2-form. As a result, *(a ^ b) is a n-2-form, which is a vector (1-form) only for n=3.
So you can define the cross product for an artibrary number of dimensions, but in general it takes two vectors and returns an n-2-form. Only in 3d is the result a vector itself.
It's also mentioned on Wiki, apparently:
https://en.wikipedia.org/wiki/Cross_product#Exterior_product
As mentioned above, the cross product can be interpreted as the exterior product in three dimensions by using the Hodge star operator to map 2-vectors to vectors. The Hodge dual of the exterior product yields an (n − 2)-vector, which is a natural generalization of the cross product in any number of dimensions.
i require a very basic a-star implementation that could be used for older python versions, so i dug through google and found one... but it is broken... (funny thing is, it is actually copied over the years by people trying to educate people on pathfinding)
my math is just not good enough to fix this simple version of a*.. i was wondering if there is a enthusiast out there that can help me out?
feel free to tag me if you're interested... thanks!
What's an a star?
I'm currently I'm using a bubble sort in my project, but it takes FOREVER to sort 10k+ lines in the currently edited file.
Here is my code: https://github.com/ayourk/HostsManager/blob/main/hostsman.py
During the bubble sort, I also check for duplicates and mess with them appropriately. Is there something I could use that is better/faster/more efficient? I don't have direct access to the editor_lines buffer, but I can move around the buffer using positions. This is a tkinter app and it mostly works.
is the function in question mnuToolSort
It's around line 650
Is there a better way that can also check for dupes?
where are you checking for duplicates?
Line 679
since there's a lot of clutter in your code, and i don't really know what's going on, could you just describe how you're doing that
Nested double for loop setup for bubble sort, the Try..except section fetches 2 consecutive lines from the buffer and splits them out into lists. Each list should contain at least 2 elements to be valid for sorting. The 2nd item in the list is compared for sorting and checked to see if it is a duplicate.
https://github.com/ayourk/HostsManager/blob/main/hostsman.py#L649
Here you're doing quite a lot of stuff while sorting, so it's not very easy to see the logic.
I would suggest doing this:
- Make a function that takes a list of hosts (as dicts/tuples/your own classes) and turns them into tkinter stuff
- Then use the built-in
sorted/list.sortto sort the list
The user has an option to exclude some lines from sorting. A series of lines can be sorted while leaving the rest of the edit buffer intact.
Sure, but you can do all the transformations with the data on the lists, not hot-swapping the graphical objects.
It will be much easier to just work with a list.
When loading the file, not all items are qualified to fit into a list. And the hosts file that I'm using is about 70k entries in addition to all the other comment lines, etc.
I'm concerned about memory usage a little.
Here you'll basically have to implement an efficient algorithm like mergesort or quicksort on scratch. Which is not very hard, but if you're going to be modifying the graphics on every swap, it's going to be very slow.
Merge sort would be my goal for this app as new files could be inserted into the buffer and I'd love to sort them/check for dupes as they are being loaded
Sorting or removing duplicates from a 70k-sized list is going to be very fast.
Certainly faster than mutating a bunch of tkinter stuff along the way
I just realize that my app is a text editor first, and went that route.
If every entry is 100 bytes, then the entire file will take up about 7 MB, which is less than half the memory needed for an interpreter on its own
If you use bytes, that is.
If you use str, it's going to be more like 30 MB
The lists/dicts still need to be transferred back and forth from the edit buffer though, right?
wdym?
Or are you saying for the sort alone, just load everything into a list and then do somelist.sort() and put things back into the edit buffer later?
What is the program supposed to be doing?
Open /etc/hosts files that can include comment lines, hosts definitions and in some cases blank lines. In mine, I have a header that is 65 lines long.
So it should take N hosts files, merge them, sort them and remove duplicate lines?
Correct, and at the same time, allowing some lines to exist as comments/extra white space outside of the specified sort range.
Why can't that be done with standard unix tools like sed and uniq?
and why do you need a GUI?
Some hosts lines have comments at the end of them (list length is > 2) and some of them don't. These are treated as unique entries when in fact they aren't. Also, I'm trying to consolidate those comments onto the duplicate host (see line 687).
the GUI is there to allow the user to make changes manually.
I see
well, the easiest and fastest way to do it would be to split the program into two parts:
- a function that applies all the transformations (sorting, filtering out lines etc.) to a list of lines
- all the GUI stuff
My problem is if I go that route, how do I know which lines/list entries I need to update when the user makes a change? I already know how to detect the modified flag of the edit buffer.
But the user only makes the changes after you've sorted and filtered the lines, right?
- The user loads the file into the edit buffer
- The user optionally chooses to sort/filter the buffer
Okay. So when the user presses the "sort" button, you can read the text from the editor widget, sort it, and then write the new contents back in.
Yeah. That's what I'm doing now. I just know my way is not very efficient. I went with bubble sort since that was the sort I could remember back from my CompSci 101 days.
No, you're sorting them in-place in the widget. What I'm suggesting is getting the text as a string, turn it into a list of lines, and sort the list (which is fast).
For sorting the list/dict, can I sort based on 1 field contained in that line using a space as a field separator?
Each line I use the str.split to at most 3 list entries and only compare dupes on the 2nd entry.
Yes. You can pass in an optional key function.
.sort() is like sorted, but in-place
so in this case xs is the list to sort? Can lists contain lists? I haven't looked that close at python's data structures yet.
If so, then I could break each line down in advance into a list and store each line in its list form.
Are you coming from a C background?
Mostly.
Yes, Python lists can contain anything. Basically, a list is just a dynamic array of pointers.
!e
users = [
{"name": "Bob", "age": 42},
{"handle": "@alice", "age": 50},
{"handle": "@charlie", "age": 19},
]
def key(user):
return user["age"]
print(sorted(users, key=key))
@austere sparrow :white_check_mark: Your eval job has completed with return code 0.
[{'handle': '@charlie', 'age': 19}, {'name': 'Bob', 'age': 42}, {'handle': '@alice', 'age': 50}]
!e
You might also want to check out dataclass, which is like a C struct
from dataclasses import dataclass
@dataclass
class User:
name: str
age: int
alice = User(name="alice", age=35)
print(alice)
print(alice.name, alice.age)
@austere sparrow :white_check_mark: Your eval job has completed with return code 0.
001 | User(name='alice', age=35)
002 | alice 35
(the type annotations don't do anything at runtime, they're just there for documentation)
So I could do key=lambda: linekey(x) and make sure in the linekey func to always compare against the 2nd list entry?
!e
You are not allowed to use that command here. Please use the #bot-commands channel instead.
(it takes a comparison function instead of a key function, but otherwise same idea)
sample data:
0.0.0.0 00fun.com
0.0.0.0 00fun.com #[Tracking.Cookie]
0.0.0.0 zzhc.vnet.cn
0.0.0.0 nabijaj-internetowo.eu```
So the plan would be to break down each line into a list causing a list such that:
hostentries = [
["0.0.0.0", "00fun.com"],
["0.0.0.0", "00fun.com", "#[Tracking.Cookie]"],
["0.0.0.0", "zzhc.vnet.cn"],
["0.0.0.0", "nabijaj-internetowo.eu"]
]```
Yes, that would work. Then you could have a function like
def key(entry):
if len(entry) < 2:
return "".join(entry)
return entry[1]
# or:
def key(entry):
if len(entry) < 2:
return "".join(entry)
host, domain, *_ = entry
return domain
I suppose you won't have any blank lines?
Correct, blank lines are considered bad for the sorted range. Also considered bad are where list items are < 2 per line.
I've also built in a check to make sure that list_item[0]'s are all equal.
And after I'm done, I put the spaces back in for the
" ".join()
Forcing invalid lines to the top/beginning of the list would be acceptable.
or maybe just remove them
Removing them could make it difficult to do a 1:1 replacement within the edit buffer?
what do you mean?
After doing the sort, the edit buffer would need to be rewritten.
Then the user can manually edit after the sort completes.
you mean, the user won't have the chance to rearrange the lines if you remove them?
reducing the number of lines will be problematic for the dialog I built. I did build in a failsafe to recheck the number of lines and update the EOL range so it isn't impossible. I think it might be best to at least bring the bad lines to the top of the range so that the user can manually delete those lines if he/she wishes.
Unless they are dupes of course. But dupe handling could be handled in 1 pass after the sort completes too.
Standalone comments and blank lines don't make much sense if you sort the lines, right? Because now there isn't necessarily a block to which the comment applies
That is why I have that beautify filter that I'll be incorporating in the selected line range before the sort. Most of that will already be done. Now if there is an easy way to combine 5 regexp's into 1, I'd really love that.
what do you mean by combining regexes?
Lines 583, 595, 607, 618, & 633 are where I filter out bad stuff from a user selected range with spinboxes.
I have to use TCL regexp's here because of tkinter
You could use |, right?
I delete text matching those regex's
foo|bar|baz = either foo, bar, or baz
AFAIK, when reading from a file Python converts all line endings to \n, but that's more of an aside
Some of these didn't work in my testing:
http://tcl.tk/man/tcl8.5/TclCmd/re_syntax.htm
Well, I don't know much about Tcl/Tk, so can't really help here. Maybe ask in #user-interfaces.
I've tried there. Last time I asked there I got crickets
I'll try that | operator and do some tests.
if that doesn't work, you can just use Python's re module
That means going back and forth with the edit buffer 😾
I'll deal with that with the sort since it is an overall improvement.
If you want an overview of Python's data structures:
https://docs.python.org/3/library/stdtypes.html
https://docs.python.org/3/library/datatypes.html
you might also be interested in this section:
https://docs.python.org/3/library/filesys.html
I've been reviewing some of those docs. I learn best by examples though.
many of those have examples, like https://docs.python.org/3/library/collections.html#defaultdict-examples
anyway, I'll go to sleep now
thanks, sleep well
@boreal depot next time you should probably open a help channel, check out #❓|how-to-get-help
I haven't much success with them either.
These topic specific channels have been fruitful though (most of the time).
Also, https://docs.python.org/3/tutorial/index.html is a pretty dense introduction to Python, and you can pick the sections you want. Not very suited for total beginners IMO, but good if you are already familiar with programming.
I think I've progressed a bit beyond that.
I'm trying to do this problem https://open.kattis.com/problems/almostperfect
This is my solution but it's too slow. What could i do to make it faster? I'm kinda new to python and only read about generators and yield keyword. Would that make it significant faster or do i need to rethink my approach to the problem?
import sys
x = sys.stdin.readlines()
for line in x:
n = line.strip('\n').split(' ')
n = int(n[0])
result = 0
for i in range(1, n):
if n % i == 0:
result = result + i
if result == n:
print(f"{n} perfect")
elif (n - result) * 1 <= 2:
print(f"{n} almost perfect")
else:
print(f"{n} not perfect")
You only have to check integers from 1 to sqrt(n)
If you find 1 divisor of n, let's say i in the range[1, sqrt(n)] then there's a corresponding divisor to i in range[sqrt(n), n] which is n / i
So the complexity would be O(sqrt(n)) instead of O(n) like you're doing
alright so i am implementing MergeSort, and i am going to see if i am understanding this correctly.
is it incorrect in describing this as:
while both lists are not empty, pop off the first elements of both, and compare them. append them to their correct list.
then, do the same thing again for both lists, since they might have leftover elements if they are not of the same length.
@glossy breach Alright thank you so much! Will try it out!
Yeah, that's correct
alright, thank you.
is it just me who is taught that python is slowest while solving problems or its just the py algo which sucks that creates a TLE, be my guest and please make me understand it properly
for competitive programming, python is very very slow, compared to other choices liike cpp
for example, you might have to write an O(n log n) algo in python, while an O(n^2) might work in cpp
thoughts?
sure
If nodes in the tree (which aren't at height 'h') are filled with both the children then, does it mean the tree is called complete binary tree ?
By slow you mean in terms of computation speed? Which would make sense because you dont have to worry about mallocing and freeing memory, which would make the process of writing code in C/C++ slow.
i have implemented QuickSort, as well.
def quicksort(source: list) -> list:
if len(source) <= 1:
return source
# Divide
source_copy = source.copy()
pivot_index = round(len(source) / 2)
pivot = source_copy.pop(pivot_index)
# Re-arrange
greater, lesser = [], []
for cmp_val in source_copy:
if cmp_val >= pivot:
greater.append(cmp_val)
else:
lesser.append(cmp_val)
# Conquer
greater = quicksort(greater)
lesser = quicksort(lesser)
# Merge
return greater + [pivot] + lesser
it works perfectly fine, but does anyone see any issues with my implementation?
i did not get this completely wrong, did i?
@brave oak hey, would you mind giving me a hand here?
if you are free, at all.
@balmy siren a "complete" binary tree means that each node has either zero, or two children.
oh wait, i might be confusing "full" with complete here.
A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.
this is what a "Complete" binary tree looks like.
🤨
Yeah that's actually a strict binary tree, where each node has either two children or none
i mean.. does a non-strict binary tree allow for one child as well, then?
Yes as long as it's not a "full or perfect"
complete, full, perfect, jeez.
In "full or perfect" binary tree, each node should have two children and all the leafs are at same level
ah alright, got it. i probably cannot answer your question then. sorry. =(
I am little confused in a complete binary tree, as you can see all the leafs aren't at same level, G at 2 and all another leafs are at 3 (counting from 0)
No worries 👍
yeah i was a bit confused at that as well.
https://web.cecs.pdx.edu/~sheard/course/Cs163/Doc/FullvsComplete.html
hmm, maybe this website is not very accurate.
But afaik, those nodes which aren't at height h (in the picture you mentioned A, B, C, D, E) should have either None or both the children..
it works perfectly fine, but does anyone see any issues with my implementation?
i did not get this completely wrong, did i?
i think i followed the description on Wikipedia pretty well, but another set of eyes is always nice in a situation like this.
Seems correct. It would be much faster if you avoided the copying and instead updated the original list in place, but this algorithm looks right.
alright, sick.
thank you!
it does not account for repeated values in the pivot, is that needed?
repeated values in the pivot? i think i saw something on the Wikipedia page relating to that.
one moment.
Ah, the return at the end should have lesser on the left and greater on the right
Otherwise you're returning the list in reverse order
alright. i will fix both of the things you brought up.
With a partitioning algorithm such as the Lomuto partition scheme described above (even one that chooses good pivot values), quicksort exhibits poor performance for inputs that contain many repeated elements. The problem is clearly apparent when all the input elements are equal: at each recursion, the left partition is empty (no input values are less than the pivot), and the right partition has only decreased by one element (the pivot is removed).

@main bison is this what you were referring to?
no, im refering to your code
pivot = source_copy.pop(pivot_index)
this is one value
my thought process was so i could do this:
return lesser + [pivot] + greater
i originally had it saved as the index of the pivot, rather than the value itself.
it just saved a line or two of code.
does that answer?
That seems fine to me. It removes the item with that index from the list, leaving any other items with the same value alone. Those will get put into greater when it loops over the list
^
it is so the loop for separating lesser and greater elements does not include the pivot itself.
sorry, i should have mentioned that.
i am not sure if that is 100% needed, though.
it was just something i added as a precaution.
what if there are 100 values of the pivot at once?

i can test that, one moment.
seems to work fine, was the 100 pivots bit hyperbole, or did you actually mean one hundred pivots?
If there's a problem with duplicate pivots, I'm not seeing it.
well, the only way to make sure is to test it a bunch of times.
i will just to make sure.
thanks, you two! if you notice anything else i have done wrong, let me know.
also i tested it with one hundred two's just to be sure.
seems to be working fine to me.
what im seeing is that for every call it ads one value as the pivot and it sorts the rest to the right.
well.. i guess it will be cought in the right hand recursion
yeah, it will.
and then empty sorted left..
left[:center] will go up before the pivot, while right[center:] will include the pivot, and everything else after.
Right. It chooses the pivot naively, and won't have the best possible performance, but it does sort correctly.
i am not looking to optimize performance (yet, of course).
but yeah, it does seem to work.
i would just get all the pivots at once
and put them in the middle like your doing
since you already know where they should be
All the adjacent ones, you mean?
if your pivot is 7
^^
is the issue with repeated pivots ones that come one after another, or just multiple occurrances of the same pivot?
and you have five 7s in your list
when seven is picked as a pivot the first time, then you know that all the sevens should be in the middle
Ah, you mean with 3 lists and an if </elif ==/else.
quicksort only sorts the pivot
so since it only sorts the pivot in the correct place, you should do that sort
oh you are right.
this seems more of a performance one, but if there are a lot of the same element than it would be a great increase to performance to just collect all of the same elements as the pivot before the actual sorting begins.
if i am interpreting what you said, correctly.
It's an interesting one. It speeds things up a lot in the case of many duplicate elements, but slows things down slightly in the case with all unique elements
yes, creating lists is very slow
I mainly use quicksort as an example to teach recursion
i think i will still do what eivl suggests here, even if it will be a bit slower.
not optimisation of quicksort
Overall, it's definitely a sensible optimization.
either way, thank you two very much for the suggested improvements.
want me to share my version of it?
yeah sure.
import random
def quick_sort(list_of_numbers):
if len(list_of_numbers) <= 1:
return list_of_numbers
pivot = random.choice(list_of_numbers)
less_then = [n for n in list_of_numbers if n < pivot]
equal = [n for n in list_of_numbers if n == pivot]
greater_then = [n for n in list_of_numbers if n > pivot]
return quick_sort(less_then) + equal + quick_sort(greater_then)
it also picks the pivot at random instead from the middle. but thats just a variant
Well... Comprehension is shorter for lines of code, but slower as well
oh right, i remember seeing that different ways of choosing the pivots have their own pros and cons.
i will also look into that tomorrow.
3 loops over the list is slower than 1 loop over it.
this is true
Slightly.
yes, but it nicely showcases recursion
alright, well i will be heading to bed now.
have a nice sleep or morning, wherever you are.
😄 morning here 😄
well, i hope you have a great morning!
goodnight, everyone.
I should go to bed too 😅
Hi wondering if someone can look at the below implementation of the remove method for linked list and I also have few questions commented
def remove(self, targetData):
current = self.head
previous = None
found = False
while(current != None):
if(current.data == targetData):
found = True
break
previous = current
current = current.next
if(found == True):
if(previous == None):
# So in this if statement we only have to do 1. Below are my comments
self.head = current.next # current.next is None
self.head = None # can we also do this?
# So would either of the above be correct?
# why cant we do the below? since current.next points None
# why do we have to do self.head = current.next
current = current.next
else:
previous.next = current.next
why cant we do:
if(previous == None):
current = current.next
When we do this it doesnt remove the last element
!e
a = [1, 2, 3]
b = a
b = [1, 2, 3, 4]
if a == [1, 2, 3, 4]:
print("b = [1, 2, 3, 4] modified the value of a")
else:
print("b = [1, 2, 3, 4] did not modify the value of a")
@flat sorrel :white_check_mark: Your eval job has completed with return code 0.
b = [1, 2, 3, 4] did not modify the value of a
Similarly, the statement current = current.next doesn't change the object referred to by self.head. Instead, it causes current to reference current.next without doing anything to self.head.
Oh ok, I see what you mean, current in that situation does not reference to self.head.
So we have to explicitly mention self.head and then point it to None.
i was wondering how i could make a basic text generator based on seeds
My inspiration: https://libraryofbabel.info/
A project towards a universal library. By this art you may contemplate the variation of the 23 letters.
hey i remember that
@sage berry my first thought? a two-way/reversible hash algorithm.
actually, googling for "Reversible Hash Function" brings up a Quora post that brings up the Library of Babel.
https://www.quora.com/Are-there-any-reversible-hash-functions
now, do i know any that you could apply to your situation? no, but it might give you a better idea on how to go further.
if you do not know, a Reversible Hash Function is essentially just a Hash algorithm that can be, well, reversed, to get the original input.
the hash you initially generate would be the seed, and you can throw text through hashing algorithm to retrieve it's index in the sequence.
hash is a java term right? isnt it the equivalent of a dictionary in python?
or is it also some kind of encoder
Hashing is used in Hashtables/Hashmaps, but it is not exclusive to Java.
Hashing is when you take one input, and then turn it into something else. (bit of an oversimplification, i suppose.)
in the world of Computer Science this is often used to convert values to numbers, like in the case of a Hashtable, but this is certainly not the extent of the use.
so yeah, i guess you could say it is like an encoder, i suppose.
ok, so i need to make a reverseable encoder
yup!
now, how you do that is up to you, though.
there are one or two things that you need to look out for. for example, not only does your Hash algorithm have to be reversible, it will also have to be "perfect."
well, if you do not want collisions, i guess.
it does not have to be, but a non-perfect hash function can produce two values that return the same hash.
hashing algorithms are meant to not be reversible
you mean i cant have 2 items be encoded to the same thing
how would reversing a hash even work
its an encoder not a 'hash'
good question, it is just an idea that came to mind.
sorry if i confused it with something else.
the first problem i run into is the encoding
if i have the seed set to 0
how do i take that and generate 410 pages or whatever
in your quora article it said something about a 273 bit hash
does that mean that the output is 273 bits
i mean, if you wanted to hash for example a string, and did so by adding up the ordinals of it's individual ASCII characters, you would need a way of extracting the individual ordinals out from the resulting number, i suppose.
*happens to be very difficult
yes, so i guess if you wanted to "reverse" it you would somehow need to attach other information.
there has to be a way to decode stuff or else all of cryptography is a joke
ahh yes, that is the right term, sorry.
yeah, you do.
which should be simple? its supposed to be bad
i suppose it depends on how you plan on having your seed be represented.
entirely numerically? base-64?
shrug, some people just choose other methods.
when someone says 256 bit encryption that means the encrypted message is always 256 bits
i believe so, yeah.
1 character is a byte right
what do you mean by character?
not necessarily.
like 'a','b'
ASCII characters, yes.
they are 2 bytes, i believe
if you include the extended table, it is one byte.
oh is the 265 bit encoded message a number that i convert into base 26 and use letters?
if you mean the standard table, it is 7 bits, but i think it is still stored in one byte.
i believe some Unicode characters are actually multi-bytes.
yes
yeah, so if you intend to allow for Unicode, that might be a bit of an issue if you use UTF-8 as your encoding.
i might be wrong there, though.
then how do you decode 256 bits into more than 32 characters
that.. is a good question. frankly i am not educated enough in cryptography to answer.
maybe is does it in 32 character blocks
ok yeah they break down the message into blocks of 4 bytes or whatever
it means the key is 256 bits
im done already lmfao
!e
print("How does this work??")
You are not allowed to use that command here. Please use the #bot-commands channel instead.
!e
print("Test again")
You are not allowed to use that command here. Please use the #bot-commands channel instead.
Can anyone join my team in hashcode?
I have zero experience and no team pls help anyone
do anyone know what data structures to look for building charts/graphs ?
Think that depends on the chart and graph probably?
No, what is this?
Lists looks okay for graphs
However you can use matrix if there are many edges between nodes
Hey i'm learning data structures and algorithms for the first time and i'm documenting it. If your just learning them, you can follow along and even contribute if your interested https://github.com/jothamsl/Algorithms-n-Data-Structures
Create the following Application.
Use for loop, while loop, and do while loops to generate a table of decimal numbers,
as well as the binary, octal, and hexadecimal equivalents of the decimal numbers,
in the range 1-256.
The program should print the results to the Console without using any of the python conversion functions.
You must design and implement the necessary algorithms for the conversion.
Make sure to ready the requirements, ask question if not sure about the requirements.
Design Details:
The Main program should prompt the user for input.
A sample output is shown below.
Note:
To generate the binary numbers your code should use a while loop to generate
the binary numbers, a “for” loop to generate the octal numbers and a “do .. while” to
generate the hexadecimal numbers.
How do I do this?
I dont understand how the while loop is used to calculate the binary numbers
especially given that the numbers are a range
not sure if this is more for this room or the data science room.i have a question about cleaning my csv data...i have this columns
Date,open,high,low,close,Volume BTC,Volume USDT,tradecount
2 0,2021-02-23 00:00:00,54087.67,54183.59,53405.5,53414.59,572.09326,30786788.62840657,16645.0
how do i clean the 0 column before 2021-02-23 but without cleaning the "date" header?
what do you mean by cleaning? what do you want to do with 0? drop the column?
Hard question for only the best data scientists haha: https://stackoverflow.com/questions/66378725/how-do-i-transfer-values-of-a-csv-files-between-certain-dates-to-another-csv-fil
Is that urs?
yah. Trying to find an answer
I think I may have overcomplicated the question though
you want to merge the two csv file right?
yes
nice comments in code.
this is a simple merge problem
just generate a join key for the second CSV based on the most recent date from the first CSV that is not later than the corresponding row’s date
then merge on that
I am having a problem that needs help in NLP.
The text "TSLA is going to the moon. I think TSLA is the greatest company ever and GM and other car manufacturers don't stand a chance when competing with TSLA" would ideally return something indicating that TSLA had positive sentiment and GM had negative sentiment.
How can I write a code in python?
for q in range(-s, s + 1):
for r in range(-s, s + 1):
if abs(q + r) <= s:
print(q, r)
``` how would you write this such that the `if` isn't there. It isn't all that needed, since over half of the pairs are correct, but just out of curiosity.
@mint jewel sth like one Range from -s,0 and the other from 0,s
Are you sure that it is {4} and {5} - not {3} and {4}?
"There is {0} and {1} but I do not like this {2}. I also know how to do {3} but {4} doesn't let me".format('apples', 'bananas', 'fruit', 'coding', 'someone')
!e
print("There is {0} and {1} but I do not like this {2}. I also know how to do {3} but {4} doesn't let me".format('apples', 'bananas', 'fruit', 'coding', 'someone'))
You are not allowed to use that command here. Please use the #bot-commands channel instead.
"There is {0} and {1} but I do not like this {2}. I also know how to do {3} but {4} doesn't let me".format(*data)
I am looking for help for my test assessment in NLP / sentiment analysis.
Task: The text "TSLA is going to the moon. I think TSLA is the greatest company ever and GM and other car manufacturers don't stand a chance when competing with TSLA" would ideally return something indicating that TSLA had positive sentiment and GM had negative sentiment.
switching back and forth from c++ to python is a trip
its like speaking english and chinese bilingually to two people all day
if you dont do it in python theres a good chance you do it in c++ lmao
how would that help?
@mint jewel Because s-s is <=s for all positive integers.
And 0
So you need to Only select negative s
In the ranges
wouldn't that exclude that say 1 1?
How would I do that?
@fervent saddle no like a tree dict
I’m thinking more about it, and maybe the trade off would actually be that you couldn’t delete from it at all
why not
It might be complicated to update the hash array which points to insertion ordered array
But maybe I’m just not thinking about it hard enough
If you deleted, and then you moved everything back in the insertion ordered array, you would have to update the hashed into array so it didn’t direct to the wrong indexes
Well, no, maybe it would be fine. It would still be O(n), it would just be a longer O(n), I think
And it might need more space
Like everything in the insertion order array would need to hold what part of the hashed into array directs to it, or something like that
who can hop on a call and check this
if int(outlist[counter][0]) + 1 == int(z[0]) or int(outlist[counter][0]) - 1 == int(z[0]) or int(outlist[counter][1]) + 1 == int(z[1]) or int(outlist[counter][1]) - 1 == int(z[1]):
how do i check if this condition is met 3 timmes

have you heard of variables, lad?
that code is super messy and does not have to be that long.
also just store the result in an array, and then run the all function on said array.
hahaha
You could probably use a counter if the statement is met and increment it to see if it ran 3 times at the end.
So when we have
def add(self, newData):
newNodeAddress = Node(newData)
newNodeAddress.next = self.head
self.head = newNodeAddress
newNodeAddress.next is actually the memory address of the previous node
so when do add(1), add(2), add(3)
Below is what's happening. Of course we are dealing with memory address, but I used values to understand what's happening
add(1)
newNodeAddress.data = 1
newNodeAddress.next.data = self.head(None)
self.head = newNodeAddress.data(1)
add(2)
newNodeAddress.data = 2
newNodeAddress.next.data = self.head(1)
self.head = newNodeAddress.data(2)
add(3)
newNodeAddress.data = 3
newNodeAddress.next.data = self.head(2)
self.head = newNodeAddress.data(3)
And we assign self.head to newNodeAddress.next first, so we can keep track of the previous memory address
Because when we add 1, 2, 3
List will end up looking like 3, 2, 1, where:
self.head.data = 3
self.head.next.data = 2
self.head.next.next.data = 1
self.head.next.next.next = None # There's no data
guys O(n!) means that it grows very fast even if n is small
example:
1 --> 90
2 --> 890
3 --> 21345
right ?
yes, O(n!) grows very fast
n!:
1 -> 1
2 -> 2
3 -> 6
4 -> 24
5 -> 120
6 -> 720
7 -> 5040
8 -> 40320
9 -> 362880
As you can see it gets bad really quickly
For an O(n!) operation on a list with 63 items, it would take 1982608315404440064116146708361898137544773690227268628106279599612729753600000000000000 operations
(minor correction: 2! is 2)
How can we difference between the caracteres and the integers in python?
How can you convert a string into a variable?
in python
var = 'string'
#if input is what you desire
var = input()
#to float it or turn it to an integer
float(var)
int(var)
thx
I am getting a "unreacheable code" when I want to do more than 2 elif statesments
What can i do?
show code
@brave oak k = "name1n"
if k == "name":
print("hi")
elif "pter":
print("hi")
elif "d":
print("hi")
you want elif k == "pter":
pycharm says to the last elif "The code is unreacheable
!e
print(bool("pter"))
@brave oak :white_check_mark: Your eval job has completed with return code 0.
True
@brave oak but when want to print it at the end i doesn't work
@brave oak the variable k
Hey, I have a question.
I want to make a class that helps with encrypted files for my project in a way that you can decrypt it with a key and edit the content only (the file is supposed to contain a 512bit salt at the beginning and content comes right after it).
Any way to make it work like a buffered reader or BytesIO?
BytesIO(file.read()) will not work because it takes a very long time to get a large file...
Thanks in advance!
I havent done a project on encryption and decryption, but I did a project on hashing(md5, and sha2) and recoded part of openssl which I suppose has a similar concept, but you just cant decrypt it. But the code I wrote took longer to run on big files, but the original openssl created a hash pretty much right away even for big files. So I would say if there's an open source project that's doing something similar to what you are doing and that project runs fast then maybe look at the source code and see how they optimized it.
What's the size of your buffer right now?
I think increasing the buffer size or chunk size when you stream data might work.
currently my buffer is 32k
32,000?
Yeah, and how big is the file you are working with is it in GB?
I'm testing it on a 6GB file which takes about 5 sec to read whole
Maybe try increasing the buffer from 32k to 64k and see if it takes less time.
maybe, ima try that rq
huh... although i think that happen because this time i ran the code on an hdd
also seconds not ms
Yeah run it on the same drive you were previously running it on that gave you 5 seconds with 32k buffer.
hdd would be slow.
yeah much better
welp i think this'll be fast enough for me, I'm not expecting larger than 20gig files
Nice! And you probably already know the trade off of doubling the buffer size.
yeah lmao
thanks!
Wondering if someone could comment on my though process of adding an element to a linked list:
#algos-and-data-structs message
#algos-and-data-structs message
can tombstones in Hashtables be overwritten, or must they remain until the next rehash?
thx for ur help
How can I insert a node after a prev_node in a Linked List, so basically the input of the function is
def insert_value(self, prev_node, new_value):
save the next_node of prev_node in a variable.
then, set the next_node of prev_node to Node(new_value) (or however you initialize a Node)
hello
then, set the next_node of that new node to the node you saved in a variable.
greetings, friend.
pip install pyaudio
File "<stdin>", line 1
pip install pyaudio
^
SyntaxError: invalid syntax
this is a channel for algorithms and datastructures.
Greetings
you need to run that command in your shell, not the Python interpreter.
how can I do it
by executing it in your shell. open your terminal and type that command. you are running it in Python, which is the wrong place.
Thank you so much, I'm doing artificial intelligence.
good luck!
thanks
no problem.
thank you
anytime.
hello everyone
i got a question about newtwon raphson algorithm
can someone help me with this equation pls
you don't define epsilon anywhere, you also need to indent the relevant part below the while loop so that it is included in that loop
hi everyone i'm a noob to big O
x = 5 + (15*20)
y = 15 -2
print(x+y)
can someone please explain why constants are O(1)?
if the duration is independent of some input size, then it is constant
so any equation with constants is just O(1)
yes, 1 + 1 will take equally long regardless of what input you have, since it doesn't use the input in any way
uhhhh should i be learning this before i learn recursion and polymorphism?
time complexity?
yes
you should tbh, it's usually intuitive any way
yep
The total number of steps T(n) wouldn't be T(1) but O() abstracts constant factors and only keeps the most important factor
why is a for loop big O(n)
sorry i'm asking noob questions
learning this for the first time
because looping over 500 elements is twice as fast as looping over a 1000 elements
the time it takes is linear to the amount of elements
yeah
now, this is not true for every for loop
if there was another loop within the outer loop, which is iterating over the list again, you'd get a O(n^2)
like
for i in my_list:
for j in my_list:
print(i == j)
for each item in the list, it has to go through the entire list again
is there a book that just teaches big O(n)? simply?
i doubt you'd find a book with only big O, but any algos text will have something on it
i'll watch some more videos and keep asking questions
If you've never covered big O in an academic setting, you can probably skip this subsection. It might confuse you more than it helps. This "FYI" is mostly here to clear up ambiguity in wording for people who have learned big O before, so that they don't say, "But I thought big O meant..:'
that's from a book...?
pick another book which does explain big O ¯_(ツ)_/¯
CLRS introduction to algorithms
Guys, there is an interview ques
if you are given some coordinatesFind the number of rectangles which can be formed,What's the key to start coding regarding this prompt
Like coordinates on a Cartesian plane
6-8
We have to find all the possible rectangle we can frame using any 4
This is a Google interview ques 👀
but it's also a question that startups have started asking too
every company wants to be FAANG
sorry didn't want to go off on a rant
An O(n^2) algorithm will always be worse than an O(n) in the long run but if the two algorithms are T(n^2 + 1) and T(100n + 1000), then the O(n^2) algorithm is actually better for n < 100
mind if I DM you a book?
🤔 intersection function seems complicated
ofc anything is appreciated
do the rectangles have to be axis aligned?
not really
These are the points through which 3 rectangles could be formed
then you can find all points with common X coordinates and all points with common Y coordinates, and then there is some clever way to do the intersection such that you get all the points
Hm
if n is small just brute force 😔
In case of this
good shit
ye, nothing wrong with a good ole
for a, b, c, d in combinations(points, 4)
lul
Will try this soon
actually, what is the big O of choose k
Question: does this have a particular name? It's not fully regular bubble-sort.
Wut
well, it is wrong regardless. It can easily go into n < 0@uneven jungle
and well, n < -len(S)
what does "worst-case scenario" even mean?
"What if we get really unlucky and the pivot is repeatedly the biggest element in the array?"
what's a pivot? never heard that term before
quicksort?
consider bubble sort. Best case scenario is that the list is already sorted, so you don't have to actually swap anything
are you guys talking to me?
for bubble sort yeah, reverse sorted is worst case
idk what quicksort or bubblesort even is
you're in the sorting algos section of your book right?
i'm in the big o notation section of cracking the coding interview
oh damn, they assume you know a bunch of stuff
maybe start with a more introductoory book then
"O (big 0): In academia, big O describes an upper bound on the time. An algorithm that prints all the values in an array could be described as O(N), but it could also be described as O(N2), O(N3), or 0(2N) (or many other big O times). The algorithm is at least as fast as each of these; therefore they are upper bounds on the runtime. This is similar to a less-than-or-equal-to relationship. If Bob is X years old (I'll assume no one lives past age 130), then you could say X .$. 130. It would also be correct to say that X .$. 1, 000 or X .$. 1,000,000. It's technically true (although not terribly useful). Likewise, a simple algorithm to print the values in an array is O(N) as well as O(N3 ) or any runtime bigger than O(N)."
this is like the first paragraph they even show about big O
ok cracking the coding interview is overrated
for beginners yes
Worst case scenario is the max number of operations required
in which case, big O doesn't
even if it might use some negative n's it still works
you tested that?
I tested it and it spat an index error at me.
In [33]: S = [random.random() for _ in range(100)]
In [34]: l = len(S) - 1
...: n = 0
...: while n < l:
...: if S[n] > S[n+1]:
...: S[n], S[n+1] = S[n+1], S[n]
...: n -= 1
...: else:
...: n += 1
...:
---------------------------------------------------------------------------
IndexError
2 n = 0
3 while n < l:
----> 4 if S[n] > S[n+1]:
5 S[n], S[n+1] = S[n+1], S[n]
6 n -= 1
IndexError: list index out of range
uhhhhhh
there has to be something that can explain big O to my beginner ass
Learn about Big O notation, an equation that describes how the run time scales with respect to some input variables. This video is a part of HackerRank's Cracking The Coding Interview Tutorial with Gayle Laakmann McDowell. http://www.hackerrank.com/domains/tutorials/cracking-the-coding-interview?utm_source=video&utm_medium=youtube&utm_campaign=ctci
trying this one
clrs algorithms is more introductory for algorithms in general
i will check it out thanks for being so helpful and supportive
you would recommend the third edition right?
whatever i found it in the third edition so i'm using it in the third edition i don't know why i asked
thanks for suggesting it
wierd, it passes my test.
!e
from random import random
L = [random() for _ in range(200)]
n = 0
l = len(L) - 1
while n < l:
if L[n] > L[n + 1]:
L[n], L[n + 1] = L[n + 1], L[n]
n-= 1
else:
n += 1
@agile sundial :x: Your eval job has completed with return code 1.
001 | Traceback (most recent call last):
002 | File "<string>", line 6, in <module>
003 | IndexError: list index out of range
what if you had a lawn of grass that's 100 by 100 and you had to mow it all
what's the runtime???
that sounds like a FAANG question to me
the question?
Yes
you can determine complexity on things that have some number or numbers which describe its input
you didn't say what is the number that changes here
Learn about Big O notation, an equation that describes how the run time scales with respect to some input variables. This video is a part of HackerRank's Cracking The Coding Interview Tutorial with Gayle Laakmann McDowell. http://www.hackerrank.com/domains/tutorials/cracking-the-coding-interview?utm_source=video&utm_medium=youtube&utm_campaign=ctci
check it out if you have time
the number that changes???
big O is an expression that describes how the runtime changes when you change the input size
well it's cracking the coding interview
she can't even explain big O notation without assuming you already know stuff
if you can't explain topics to me like a 5 year old i'm not reading the book sorry
HMM
the 5 year old strategy is from TAs at my college
lets actually state a good question
what is time complexity of mowing a square lawn with a side of N
that is better but i don't know how to answer that either
she said it's O(a) and a is the area of grass
or it's O(s^2) which is the length on one side
that is also correct
well, how much longer does it take to mow 100 Ares than 50 Ares
100*100 v 50 *50?
an Are is a unit of area
i mean, algorithms are not a beginner topic. you kinda have to assume your audience understands basic programming topics
i do understand basic programming topics...... I think
Acre?
Are is also correct
@old rover then you only have the fun left, good for you
is this sarcasm? i detect sarcasm
alright, new question
how much longer does it take to mow 1000 square meters than 500 square meters
i can't upload images here right?
you can
ok hang on
at least I think so
this is in C++? or C? or C#? idk
but why is it O(a*b) instead of O(N^2)
bc of the inputs???
well, what would be N in O(N^2)
I guess cpp
Yee, I have seen it , too many curly brackets {}{}} in a pseudocode
Like Google interviews
Lol 👀
google interviews got every damn company thinking they're google
anyways i still don't get why it's O(a*b)
well, what do you think it is?
i don't think it's O(N^2) bc it's not a nested for loop
🤓 aight , see u guys after understanding data structures
peace out dude
Bai
there is a for loop inside of a for loop
ok then i don't get it at all
O(a*b)
well, you have 2 inputs
both of which affect the time
so the O obviously has to respect both
good question
big O notation is a way to describe how expensive something is for really large inputs
In academia, big O describes an upper bound on the time. An algorithm that prints all the values in an array could be described as O(N), but it could also be described as O(N2), O(N3), or 0(2N) (or many other big O times). The algorithm is at least as fast as each of these; therefore they are upper bounds on the runtime. This is similar to a less-than-or-equal-to relationship. If Bob is X years old (I'll assume no one lives past age 130), then you could say X .$. 130. It would also be correct to say that X .$. 1, 000 or X .$. 1,000,000. It's technically true (although not terribly useful). Likewise, a simple algorithm to print the values in an array is O(N) as well as O(N3 ) or any runtime bigger than O(N).
its not as useful as people try to make you believe
there you go that's straight from cracking the coding interierw
Clipboard huh
you're right, but you're supposed to show off how smart you are to interviewers by talking about this
