Skip to content

Instantly share code, notes, and snippets.

@harpiechoise
Created March 28, 2020 03:04
Show Gist options
  • Select an option

  • Save harpiechoise/c0e5dd48b0140a5e9542f38c295c79b3 to your computer and use it in GitHub Desktop.

Select an option

Save harpiechoise/c0e5dd48b0140a5e9542f38c295c79b3 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "raw",
"metadata": {},
"source": [
"5\n",
"2 3 4 1 5\n",
"Sample Output 1\n",
"\n",
"3\n",
"Explanation 1\n",
"\n",
"Given array \n",
"After swapping we get \n",
"After swapping we get \n",
"After swapping we get \n",
"So, we need a minimum of swaps to sort the array in ascending order.\n",
"\n",
"Sample Input 2\n",
"\n",
"7\n",
"1 3 5 2 4 6 7\n",
"Sample Output 2\n",
"\n",
"3"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"from typing import Tuple, List\n",
"Result = Tuple[List[int], int]\n",
"def swap_counter(numeros_iniciales: list) -> Result:\n",
" \"\"\" \n",
" This function takes a list and returns a sorted list, and counts\n",
" The number of insertions\n",
" @arg numeros_iniciales\n",
" A randomly unsorted list\n",
" returns\n",
" A Result Type that consist in a sorted list of integers, and the number of insertions\n",
" or swaps\n",
" \"\"\"\n",
" swaps = 0\n",
" for i in range(1, len(numeros_iniciales)):\n",
" # Extraemos el item a insertar\n",
" item_a_insertar = numeros_iniciales[i]\n",
" # Necesitamos retener la referencia del item anterior para saber en que \n",
" # Posicion insertarlo\n",
" j = i - 1\n",
" # Movemos todos los numeros hacia el lugar donde debe ser insertado\n",
" # Comprobamos donde insertamos el item\n",
" while j >= 0 and numeros_iniciales[j] > item_a_insertar:\n",
" numeros_iniciales[j + 1] = numeros_iniciales[j]\n",
" j -= 1\n",
" \n",
" # Insertamos el item\n",
" swaps += 1\n",
"\n",
" numeros_iniciales[j + 1] = item_a_insertar\n",
" # Contamos la insercion\n",
" return numeros_iniciales, swaps"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"# Sample Input 1\n",
"assert swap_counter([2, 3, 4, 1, 5]) == ([1, 2, 3, 4, 5], 3)\n",
"# Sample input 2\n",
"assert swap_counter([1, 3, 5, 2, 4, 6, 7]) == ([1, 2, 3, 4, 5, 6, 7], 3)"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [],
"source": [
"# Documentacion\n",
"Factors = List[int]\n",
"def factors(numero: int) -> Factors:\n",
" \"\"\" \n",
" This function takes a number and return his factor decomposition\n",
" \n",
" @arg numero\n",
" Some integer number\n",
" \n",
" Returns\n",
" All prime factors of a number passed to the function\n",
" \"\"\"\n",
" \n",
" # Tomamos el primer numero primo que tenemos que es 2\n",
" i = 2 \n",
" factores = []\n",
" # Loop infinito\n",
" while True: \n",
" # Mientras que n % i == 0\n",
" while numero % i == 0:\n",
" # Agregamos 1 al contador\n",
" counter = counter + 1\n",
" factores.append(i)\n",
"\n",
" # Dividimos el numero por el factor primo\n",
" numero = numero / i\n",
"\n",
" # Aumentamos el factor en 1\n",
" i = i + 1\n",
" # Si el numero inicial ya es 1, se retornan los factores\n",
" if numero == 1:\n",
" return factores "
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [],
"source": [
"# Test primero\n",
"assert factors(15) == [3, 5]\n",
"assert factors(32) == [2, 2, 2, 2, 2]"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[2, 2, 2, 2, 2]"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment