Skip to content

Instantly share code, notes, and snippets.

View ILoveBacteria's full-sized avatar

Moein Arabi ILoveBacteria

View GitHub Profile
@ILoveBacteria
ILoveBacteria / string_generator.py
Created December 30, 2022 18:28
A simple Python function that can generate a random string
import random
import string
# whitespace – a string containing all ASCII whitespace
# ascii_lowercase – a string containing all ASCII lowercase letters
# ascii_uppercase – a string containing all ASCII uppercase letters
# ascii_letters – a string containing all ASCII letters
# digits – a string containing all ASCII decimal digits
# hexdigits – a string containing all ASCII hexadecimal digits
@ILoveBacteria
ILoveBacteria / dynamic_array.cpp
Created December 31, 2022 19:49
How to change the size of dynamic array in c++(like realloc in c)
#define MIN(a, b) a < b ? a : b
void changeSize(int* &arr, int &size, int newSize) {
// Allocate a new array
int* newArray = new int[newSize];
// Copy all elements to the new array
for(int i = 0; i < MIN(size, newSize); ++i) {
newArray[i] = arr[i];
}
// Free and assign the new Array
@ILoveBacteria
ILoveBacteria / countSubTree.java
Created January 9, 2023 17:00
This method counts the number of sub trees from a custom root
class BST_Tree {
Node nodeHead;
/**
* @param customRoot The custom node is a root that will be calculate number of sub trees from that
*/
int countSubTree(Node customRoot) {
if (customRoot.left != null && customRoot.right != null) {
int subTreeLeft = countSubTree(customRoot.left);
int subTreeRight = countSubTree(customRoot.right);
@ILoveBacteria
ILoveBacteria / views.py
Created March 4, 2023 20:03
How to write a view in Django
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
@ILoveBacteria
ILoveBacteria / mysite_urls.py
Created March 4, 2023 20:05
Create paths in Django
# mysite/urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
@ILoveBacteria
ILoveBacteria / polls_templates_polls_detail.html
Last active March 8, 2023 14:26
How to write a minimal form in Django
<!-- polls/templates/polls/detail.html -->
<form action="{% url 'polls:vote' question.id %}" method="post">
{% csrf_token %}
<fieldset>
<legend><h1>{{ question.question_text }}</h1></legend>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
{% for choice in question.choice_set.all %}
<input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}">
<label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br>
{% endfor %}
@ILoveBacteria
ILoveBacteria / forms.py
Last active March 8, 2023 14:25
A simple form in Django
from django import forms
class NameForm(forms.Form):
your_name = forms.CharField(label='Your name', max_length=100)
@ILoveBacteria
ILoveBacteria / App.js
Last active March 8, 2023 14:36
How to handle events correctly in React
class App extends React.Component {
constructor(props) {
super(props);
this.chooseVideo = this.chooseVideo.bind(this);
}
chooseVideo(newVideo) {
this.setState({
// Change something
});
@ILoveBacteria
ILoveBacteria / request.js
Last active March 10, 2023 19:33
How to send a get request in JavaScript
async function getData(url) {
const headers = {} // Add headers here
const init = {
method: 'GET',
headers: new Headers(headers),
}
const response = await fetch(url, init);
if (response.ok) {
return await response.json();
}
@ILoveBacteria
ILoveBacteria / client.dart
Created June 29, 2023 15:05
Request and response through socket in Dart and Java
import 'dart:convert';
import 'dart:core';
import 'dart:io';
class MySocket {
final int port = 5000;
final String host = "10.0.2.2";
final String data;
MySocket(this.data);