Mad Library Sesh

This commit is contained in:
Luke Else 2023-01-05 14:02:29 +00:00
parent 41ab2680ff
commit 9963b8ffd4
6 changed files with 348 additions and 0 deletions

31
LibStudySesh.sln Normal file
View File

@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.3.32929.385
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "LibStudySesh", "LibStudySesh.vcxproj", "{3D037F4C-2040-4E91-880E-335F27A3A8ED}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{3D037F4C-2040-4E91-880E-335F27A3A8ED}.Debug|x64.ActiveCfg = Debug|x64
{3D037F4C-2040-4E91-880E-335F27A3A8ED}.Debug|x64.Build.0 = Debug|x64
{3D037F4C-2040-4E91-880E-335F27A3A8ED}.Debug|x86.ActiveCfg = Debug|Win32
{3D037F4C-2040-4E91-880E-335F27A3A8ED}.Debug|x86.Build.0 = Debug|Win32
{3D037F4C-2040-4E91-880E-335F27A3A8ED}.Release|x64.ActiveCfg = Release|x64
{3D037F4C-2040-4E91-880E-335F27A3A8ED}.Release|x64.Build.0 = Release|x64
{3D037F4C-2040-4E91-880E-335F27A3A8ED}.Release|x86.ActiveCfg = Release|Win32
{3D037F4C-2040-4E91-880E-335F27A3A8ED}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {9AEFFC5C-BDFA-4B81-BC50-F62BD1935871}
EndGlobalSection
EndGlobal

139
LibStudySesh.vcxproj Normal file
View File

@ -0,0 +1,139 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{3d037f4c-2040-4e91-880e-335f27a3a8ed}</ProjectGuid>
<RootNamespace>LibStudySesh</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="src\leet.cpp" />
<ClCompile Include="src\main.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\leet.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\main.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\leet.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\leet.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

34
src/leet.cpp Normal file
View File

@ -0,0 +1,34 @@
#pragma once
#include "leet.h"
std::vector<std::vector<int>> Solution::threeSum(std::vector<int>& nums) {
std::vector<std::vector<int>> ans;
//itterate through map O(n^2) and create a map of the sums of each element
for (int i = 0; i < nums.size(); i++)
{
for (int j = i+1; j < nums.size(); j++)
{
sums[nums[i] + nums[j]] = std::vector<int>{i, j};
}
}
for (int i = 0; i < nums.size(); i++)
{
if (sums.count(0-nums[i]))
{//If required value is in map
std::vector<int> indexes = sums[0-nums[i]];
if (i == indexes[0] || i == indexes[1])
continue;
ans.push_back({ nums[i], nums[indexes[0]], nums[indexes[1]] });
}
}
return ans;
}
//
//Given an integer array nums, return all the triplets[nums[i], nums[j], nums[k]]
// such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.
//
//Notice that the solution set must not contain duplicate triplets.

11
src/leet.h Normal file
View File

@ -0,0 +1,11 @@
#pragma once
#include <vector>
#include <map>
class Solution {
public:
std::vector<std::vector<int>> threeSum(std::vector<int>& nums);
private:
//Map of sum + indexes
std::map<int, std::vector<int>> sums;
};

103
src/main.cpp Normal file
View File

@ -0,0 +1,103 @@
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <iomanip>
#include "leet.h"
template<typename T>
void swap(T* xp, T* yp) {
T temp = *xp;
*xp = *yp;
*yp = temp;
}
void selectionSort(int caseArr[], std::string placeArr[], int n)
{
int i, j, min_idx;
for (i = 0; i < n - 1; i++)
{
min_idx = i;
for (j = i + 1; j < n; j++)
if (caseArr[j] > caseArr[min_idx])
min_idx = j;
swap<int>(&caseArr[min_idx], &caseArr[i]);
swap<std::string>(&placeArr[min_idx], &placeArr[i]);
}
}
/*
Write code to replace the selection sort function with bubble sort. In
your own words, briefly compare the two sorting algorithms. Use the
code examples to support your claims.
*/
void bubbleSort(int caseArr[], std::string placeArr[], int n)
{
bool flag = true;
while (flag) {
for (int i = n - 1; i > 0; i--)
{
if (caseArr[i] > caseArr[i - 1])
{//Swap needs to take place
flag = false;
swap<int>(&caseArr[i], &caseArr[i - 1]);
swap<std::string>(&placeArr[i], &placeArr[i - 1]);
i+=2;
}
}
//Array is sorted
if (flag)
return;
flag = true;
}
}
/*
Bubble sort has an average time complexity of O(n^2) which is the same as an insertion
sort so as the number of data items grow, both are likely to have same scale in performance.
This means it doesn't really matter which one we pick. They both also have the same memory complexity of
O(1) which is constant as no new memory is allocated during the execution of the program. Because of this,
both algorithms are nearly idendical hence it doesn't really matter which one we choose.*/
/*
Birmingham 120
Derbyshire 94
Essex 182
Hertfordshire 174
Kent 157
Liverpool 102
Northamptonshire 107
Nottinghamshire 88
Staffordshire 89
Wolverhampton 89
*/
int main() {
/*std::string UTLANames[] = { "Birmingham", "Derbyshire", "Essex",
"Hertfordshire", "Kent", "Liverpool",
"Northamptonshire", "Nottinghamshire",
"Staffordshire", "Wolverhampton" };
int CovidCases[] = { 120, 94, 182, 174, 157, 102, 107, 88, 89, 89 };
bubbleSort(CovidCases, UTLANames, 10);
for (size_t i = 0; i < 10; i++)
{
std::cout << std::left << std::setw(20) << UTLANames[i] << CovidCases[i] << " Cases\n";
}
std::cout << "Hello, World!";*/
std::vector<int> nums{ -1, 0, 1, 2, -1, -4 };
Solution s;
std::vector<std::vector<int>> ans = s.threeSum(nums);
}