Skip to content

Commit 6d4a842

Browse files
committed
Ch_05._Bit_Manipulation.Q05 01 - Insertion
1 parent 856e599 commit 6d4a842

10 files changed

+96
-7
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.vs
22
bin
33
obj
4+
.ionide

Ch 01. Arrays and Strings/Q1 07 Rotate Matrix.fs

-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ type ``Q1 07 - Rotate Matrix``() =
2626

2727
// top -> right
2828
matrix.[i, last] <- top // right <- saved top
29-
|> ignore
3029

3130
let rotateMatrixByRevTranspose m = m |> List.rev |> List.transpose
3231

@@ -61,4 +60,3 @@ type ``Q1 07 - Rotate Matrix``() =
6160
AssortedMethods.PrintIntListListMatrix (rotateMatrixByRevTranspose matrix)
6261
printfn ""
6362
AssortedMethods.PrintIntListListMatrix (rotateMatrixByRevCustomTranspose matrix)
64-
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
<RootNamespace>Ch_05._Bit_Manipulation</RootNamespace>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<Compile Include="Q05 01 Insertion.fs" />
10+
</ItemGroup>
11+
12+
<ItemGroup>
13+
<ProjectReference Include="..\ctci.Contracts\ctci.Contracts.fsproj" />
14+
<ProjectReference Include="..\ctci.Library\ctci.Library.fsproj" />
15+
</ItemGroup>
16+
17+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
namespace Ch_05._Bit_Manipulation
2+
3+
open ctci.Contracts
4+
open ctci.Library
5+
6+
type ``Q05 01 - Insertion``() =
7+
inherit Question()
8+
9+
let updateBits n m i j =
10+
seq {
11+
if (i > j || i < 0 || j >= 32) then
12+
yield 0
13+
14+
(* Create a mask to clear bits i through j in n
15+
* EXAMPLE: i = 2, j = 4. Result should be 11100011.
16+
* (Using 8 bits for this example. This is obviously not actually 8 bits.)
17+
*)
18+
let allOnes = ~~~0 // allOnes = 11111111
19+
20+
let left = if (j < 31) then (allOnes <<< (j + 1)) else 0 // 1s until position j, then 0s. left = 11100000
21+
let right = ((1 <<< i) - 1) // 1s after position i. right = 00000011
22+
let mask = left ||| right // All 1s, except for 0s between i and j. mask = 11100011
23+
24+
(* Clear i through j, then put m in there *)
25+
let n_cleared = n &&& mask // Clear bits j through i.
26+
let m_shifted = m <<< i // Move m into correct position.
27+
28+
(* OR them, and we're done! *)
29+
yield n_cleared ||| m_shifted
30+
} |> Seq.head
31+
32+
override this.Run() =
33+
let a = ~~~23423
34+
printfn "%s" (AssortedMethods.toFullBinaryString a)
35+
let b = 5
36+
printfn "%s" (AssortedMethods.toFullBinaryString b)
37+
let c = updateBits a b 29 31
38+
printfn "%s" (AssortedMethods.toFullBinaryString c)
39+
40+
printfn ""
41+
let a1 = 1024
42+
printfn "%s" (AssortedMethods.toFullBinaryString a1)
43+
let b1 = 19
44+
printfn "%s" (AssortedMethods.toFullBinaryString b1)
45+
let c1 = updateBits a1 b1 2 6
46+
printfn "%s" (AssortedMethods.toFullBinaryString c1)
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"folders": [
3+
{
4+
"path": "."
5+
}
6+
],
7+
"settings": {}
8+
}

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
- [ ] 4.12 - Paths with Sum
5353

5454
#### Chapter 5
55-
- [ ] 5.1 - Insertion
55+
- [x] 5.1 - Insertion
5656
- [ ] 5.2 - Binary to String
5757
- [ ] 5.3 - Flip Bit To Win
5858
- [ ] 5.4 - Next Number

ctci.Console/Program.fs

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
open ctci.Contracts
22
open Ch_01._Arrays_and_Strings
3+
open Ch_05._Bit_Manipulation
34
open Ch_10._Sorting_and_Searching
45

56
[<EntryPoint>]
67
let main argv =
78

89
let chapters : Question [] [] = [|
910
[|
10-
new ``Q1 01 - Is Unique``()
11-
new ``Q1 07 - Rotate Matrix``()
11+
``Q1 01 - Is Unique``()
12+
``Q1 07 - Rotate Matrix``()
1213
|]; [|
13-
new ``Q10 01 - Sorted Merge``()
14+
``Q05 01 - Insertion``()
15+
|]; [|
16+
``Q10 01 - Sorted Merge``()
1417
|]
1518
|]
1619

ctci.Console/ctci.Console.fsproj

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<ItemGroup>
1313
<ProjectReference Include="..\ctci.Library\ctci.Library.fsproj" />
1414
<ProjectReference Include="..\Ch 01. Arrays and Strings\Ch 01. Arrays and Strings.fsproj" />
15+
<ProjectReference Include="..\Ch 05. Bit Manipulation\Ch 05. Bit Manipulation.fsproj" />
1516
<ProjectReference Include="..\Ch 10. Sorting and Searching\Ch 10. Sorting and Searching.fsproj" />
1617
</ItemGroup>
1718

ctci.Library/AssortedMethods.fs

+8
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,11 @@ module AssortedMethods =
3333
for j in 0 .. (matrix.[i] |> List.length) - 1 do
3434
matrix.[i].[j] |> PrintMatrixItem
3535
printfn ""
36+
37+
let toFullBinaryString a =
38+
let rec toBin value len =
39+
match len > 1 with
40+
| true -> toBin (value >>> 1) (len - 1)
41+
| _ -> ""
42+
|> sprintf "%s%c" <| ("01".[value &&& 1])
43+
toBin a 32

ctci.sln

+8-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "ctci.Contracts", "ctci.Cont
1818
EndProject
1919
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Ch 01. Arrays and Strings", "Ch 01. Arrays and Strings\Ch 01. Arrays and Strings.fsproj", "{2B3E758F-CC34-4B21-A294-164841E3173E}"
2020
EndProject
21-
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Ch 10. Sorting and Search", "Ch 10. Sorting and Searching\Ch 10. Sorting and Searching.fsproj", "{32019E42-0879-419F-A366-02ABD24D442B}"
21+
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Ch 10. Sorting and Searching", "Ch 10. Sorting and Searching\Ch 10. Sorting and Searching.fsproj", "{32019E42-0879-419F-A366-02ABD24D442B}"
22+
EndProject
23+
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Ch 05. Bit Manipulation", "Ch 05. Bit Manipulation\Ch 05. Bit Manipulation.fsproj", "{07FAD3BD-2406-488D-B3C1-7035C8766B11}"
2224
EndProject
2325
Global
2426
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -46,6 +48,10 @@ Global
4648
{32019E42-0879-419F-A366-02ABD24D442B}.Debug|Any CPU.Build.0 = Debug|Any CPU
4749
{32019E42-0879-419F-A366-02ABD24D442B}.Release|Any CPU.ActiveCfg = Release|Any CPU
4850
{32019E42-0879-419F-A366-02ABD24D442B}.Release|Any CPU.Build.0 = Release|Any CPU
51+
{07FAD3BD-2406-488D-B3C1-7035C8766B11}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
52+
{07FAD3BD-2406-488D-B3C1-7035C8766B11}.Debug|Any CPU.Build.0 = Debug|Any CPU
53+
{07FAD3BD-2406-488D-B3C1-7035C8766B11}.Release|Any CPU.ActiveCfg = Release|Any CPU
54+
{07FAD3BD-2406-488D-B3C1-7035C8766B11}.Release|Any CPU.Build.0 = Release|Any CPU
4955
EndGlobalSection
5056
GlobalSection(SolutionProperties) = preSolution
5157
HideSolutionNode = FALSE
@@ -56,6 +62,7 @@ Global
5662
{C21A4F46-F60C-47B3-9CD8-4AEB9B1C9923} = {3AD9202C-AE28-4B77-8BA8-2F93E767DE93}
5763
{2B3E758F-CC34-4B21-A294-164841E3173E} = {0C6F0C29-F67E-4382-9E01-EBE71E2002BE}
5864
{32019E42-0879-419F-A366-02ABD24D442B} = {0C6F0C29-F67E-4382-9E01-EBE71E2002BE}
65+
{07FAD3BD-2406-488D-B3C1-7035C8766B11} = {0C6F0C29-F67E-4382-9E01-EBE71E2002BE}
5966
EndGlobalSection
6067
GlobalSection(ExtensibilityGlobals) = postSolution
6168
SolutionGuid = {1DCE92E9-06E9-4B99-AB0D-E96318853900}

0 commit comments

Comments
 (0)