Files
uni/third/semester1/CT3532: Database Systems II/assignments/assignment2/latex/_minted-main/DC295A307E71BF1DF365F740E3AEFDD13BE8E12346291E9D24029A6B880975F3.pygtex
2023-12-07 01:44:01 +00:00

30 lines
2.3 KiB
Plaintext

\begin{Verbatim}[commandchars=\\\{\}]
\PYG{c+c1}{\PYGZsh{} function to return the times when two given players are in the same location}
\PYG{c+c1}{\PYGZsh{} input: player ID of both players}
\PYG{c+c1}{\PYGZsh{} output: array of timestamps}
\PYG{k}{def} \PYG{n+nf}{same\PYGZus{}times}\PYG{p}{(}\PYG{n}{player1}\PYG{p}{,} \PYG{n}{player2}\PYG{p}{,} \PYG{n}{grid}\PYG{p}{):}
\PYG{c+c1}{\PYGZsh{} get the times both players are in the given grid}
\PYG{n}{player1\PYGZus{}times} \PYG{o}{=} \PYG{n}{query}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}select times from table @ \PYGZdq{}} \PYG{o}{+} \PYG{n+nb}{hash}\PYG{p}{(}\PYG{n}{player1}\PYG{p}{,} \PYG{n}{grid}\PYG{p}{))}
\PYG{n}{player2\PYGZus{}times} \PYG{o}{=} \PYG{n}{query}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}select times from table @ \PYGZdq{}} \PYG{o}{+} \PYG{n+nb}{hash}\PYG{p}{(}\PYG{n}{player2}\PYG{p}{,} \PYG{n}{grid}\PYG{p}{))}
\PYG{n}{same\PYGZus{}times} \PYG{o}{=} \PYG{p}{[]}
\PYG{c+c1}{\PYGZsh{} index i for player1\PYGZus{}times and index j for player2\PYGZus{}times}
\PYG{n}{i} \PYG{o}{=} \PYG{n}{j} \PYG{o}{=} \PYG{l+m+mi}{0}
\PYG{k}{while} \PYG{n}{i} \PYG{o}{\PYGZlt{}} \PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{player1\PYGZus{}times}\PYG{p}{)} \PYG{o+ow}{and} \PYG{n}{j} \PYG{o}{\PYGZlt{}} \PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{player2\PYGZus{}times}\PYG{p}{):}
\PYG{k}{if} \PYG{n}{player1\PYGZus{}times}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]} \PYG{o}{==} \PYG{n}{player2\PYGZus{}times}\PYG{p}{[}\PYG{n}{j}\PYG{p}{]:}
\PYG{c+c1}{\PYGZsh{} both players are in the grid cell at this time}
\PYG{n}{same\PYGZus{}times}\PYG{o}{.}\PYG{n}{append}\PYG{p}{(}\PYG{n}{player1\PYGZus{}times}\PYG{p}{[}\PYG{n}{i}\PYG{p}{])}
\PYG{n}{i} \PYG{o}{+=} \PYG{l+m+mi}{1}
\PYG{n}{j} \PYG{o}{+=} \PYG{l+m+mi}{1}
\PYG{k}{else} \PYG{k}{if} \PYG{n}{player2\PYGZus{}times}\PYG{p}{[}\PYG{n}{j}\PYG{p}{]} \PYG{o}{\PYGZgt{}} \PYG{n}{player1\PYGZus{}times}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]:}
\PYG{c+c1}{\PYGZsh{} increment the player1 index (i) as it\PYGZsq{}s smaller than the time at j}
\PYG{n}{i} \PYG{o}{+=} \PYG{l+m+mi}{1}
\PYG{k}{else}\PYG{p}{:}
\PYG{c+c1}{\PYGZsh{} increment the player2 index (j) as it\PYGZsq{}s smaller than the time at i}
\PYG{n}{j} \PYG{o}{+=} \PYG{l+m+mi}{1}
\PYG{k}{return} \PYG{n}{same\PYGZus{}times}
\end{Verbatim}