recursion

This commit is contained in:
Vladimir Lemus 2023-03-15 13:55:10 -06:00
parent 472deec397
commit b399ea5dc4
4 changed files with 100 additions and 0 deletions

47
cesar.hs Normal file
View File

@ -0,0 +1,47 @@
import Data.Char
lowers :: String -> Int
lowers xs = length [x | x <- xs, x >= 'a' && x <= 'z']
count :: Char -> String ->Int
count x xs = length [x' | x' <- xs, x==x']
positions :: Eq a => a -> [a] -> [Int]
positions x xs = [i | (x',i) <- zip xs [0..n], x == x']
where n = length xs - 1
let2int :: Char -> Int
let2int c = ord c - ord 'a' --caracteres a unicode
int2let :: Int -> Char
int2let n = chr (ord 'a' + n)
shift :: Int -> Char -> Char
shift n c | isLower c = int2let ((let2int c + n) `mod` 26)
| otherwise = c
cifrar :: Int -> String -> String
cifrar n xs = [shift n x | x <- xs]
table :: [Float]
table = [11.72,1.49, 3.87, 4.67,13.72,0.69,1.00,1.18,5.28,0.52,0.11,5.24,3.08,6.83,8.44,2.89,1.11,6.41,7.20,4.60,4.55,1.05,0.04,0.14,1.09,0.47]
porciento :: Int -> Int -> Float --fromIntegral :: Int a Float
porciento n m = (fromIntegral n / fromIntegral m) * 100
frec :: String -> [Float]
frec xs = [porciento (count x xs) n | x <- ['a'..'z']]
where n = lowers xs
chisqr :: [Float] -> [Float] -> Float
chisqr os es = sum [((o-e)^2)/e | (o,e) <- zip os es]
rotate :: Int -> [a] -> [a]
rotate n xs = drop n xs ++ take n xs
crack :: String -> String
crack xs = cifrar (-factor) xs
where
factor = head (positions (minimum chitab) chitab)
chitab = [chisqr (rotate n table') table | n <- [0..25]]
table' = frec xs

7
recursion.hs Normal file
View File

@ -0,0 +1,7 @@
import Data.Set as Set
doRep x n | n <= 0 = []
| otherwise = x:doRep x (n-1)
miLista = [1,4,8,2,3,3,5,1,6]
miConj = Set.fromList(miLista)

21
recursion.py Normal file
View File

@ -0,0 +1,21 @@
def doRep(x, n):
y = []
if n==0:
return []
else:
y = doRep(x,n-1)
print(y)
y.append(x)
return y
def lSum(list):
if not list:
return 0
else:
return list[0]+lSum(list[1:])
lSum2 = lambda list: 0 if not list \
else list[0] + lSum2(list[1:])
miLista = [1,4,8,2,3,3,5,1,6]
miConj = set(miLista)

25
tarea2.tex Normal file
View File

@ -0,0 +1,25 @@
\documentclass[10pt,a4paper]{article}
\usepackage[spanish]{babel}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{hyperref}
\usepackage{amsfonts}
\usepackage{amssymb}
%\usepackage{braket}
\author{Programación funcional para la física computacional}
\title{Tarea 2}
\begin{document}
\maketitle
\begin{enumerate}
\item Define la magnitud de la fuierza gravitacional como una función \emph{curriada}
\begin{equation*}
|\vec{F}|= -G\frac{M\cdot m}{r^2}
\end{equation*}
(puede ser en python o Haskell, donde prefieras) y úsala para calcular la fuerza que ejerce la Tierra sobre ti (puedes dar un aproximado de tu peso), la Luna y sobre Neptuno (usa como constantes lo que no vayta a cambiar y sólo da como argumentos lo que si cambie para estos casos.).
\item En Haskell define el operador producto punto y cálcula el área formada entre los vectores $(1,2,3)$ y $(2,3,1)$. Puedes definir los vectores como listas.
\item De un eexperimento del laboratorio de mecánica te dan la si
\end{enumerate}
\end{document}