Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
0
02-ifm1-refactoring-challenge
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Kai Renz
02-ifm1-refactoring-challenge
Commits
4cb74637
Commit
4cb74637
authored
1 year ago
by
Kai Renz
Browse files
Options
Downloads
Patches
Plain Diff
Feature: Added a possible Solution
Refactored the Method fl to show what is actually going on.
parent
4c304b18
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
fl.cpp
+55
-25
55 additions, 25 deletions
fl.cpp
main.cpp
+10
-10
10 additions, 10 deletions
main.cpp
with
65 additions
and
35 deletions
fl.cpp
+
55
−
25
View file @
4cb74637
//
// Created by Kai Renz on 02.11.23.
static
const
int
UNUSED
=
-
1
;
//
bool
canSwap
(
const
int
*
array
,
int
length
,
int
valueAtPosition
);
int
fl
(
int
*
i
,
int
l
)
{
int
doSwap
(
int
*
array
,
int
valueAtPosition
);
for
(
int
k
=
0
;
k
<
l
;
k
++
)
{
if
(
i
[
k
]
<
0
||
i
[
k
]
>=
l
)
{
void
doStuffAtPosition
(
int
*
array
,
int
length
,
int
position
);
i
[
k
]
=-
1
;
}
else
{
void
handleValidValueAtPosition
(
int
*
array
,
int
length
,
int
position
);
if
(
i
[
k
]
!=
k
)
{
int
v
=
i
[
k
];
bool
hasUnallowedValueAtPosition
(
const
int
*
array
,
int
length
,
int
position
);
if
(
i
[
k
]
!=
k
)
{
i
[
k
]
=-
1
;
int
findeLueckeInArray
(
int
*
array
,
int
length
)
{
}
for
(
int
position
=
0
;
position
<
length
;
position
++
)
{
while
(
v
>=
0
&&
v
<
l
&&
i
[
v
]
!=
v
)
{
doStuffAtPosition
(
array
,
length
,
position
);
int
t
=
i
[
v
];
}
i
[
v
]
=
v
;
int
positionLuecke
=
0
;
v
=
t
;
for
(;
positionLuecke
<
length
&&
array
[
positionLuecke
]
!=
UNUSED
;
positionLuecke
++
)
{}
}
return
positionLuecke
;
}
}
}
void
doStuffAtPosition
(
int
*
array
,
int
length
,
int
position
)
{
if
(
hasUnallowedValueAtPosition
(
array
,
length
,
position
))
{
array
[
position
]
=
UNUSED
;
return
;
}
}
int
m
=
0
;
for
(;
m
<
l
&&
i
[
m
]
!=-
1
;
m
++
)
{}
if
(
array
[
position
]
==
position
)
{
return
m
;
return
;
}
}
\ No newline at end of file
handleValidValueAtPosition
(
array
,
length
,
position
);
}
bool
hasUnallowedValueAtPosition
(
const
int
*
array
,
int
length
,
int
position
)
{
return
array
[
position
]
<
0
||
array
[
position
]
>=
length
;
}
void
handleValidValueAtPosition
(
int
*
array
,
int
length
,
int
position
)
{
int
valueAtPosition
=
array
[
position
];
array
[
position
]
=
UNUSED
;
while
(
canSwap
(
array
,
length
,
valueAtPosition
))
{
valueAtPosition
=
doSwap
(
array
,
valueAtPosition
);
}
}
int
doSwap
(
int
*
array
,
int
valueAtPosition
)
{
int
valueAtFuturePosition
=
array
[
valueAtPosition
];
array
[
valueAtPosition
]
=
valueAtPosition
;
return
valueAtFuturePosition
;
}
bool
canSwap
(
const
int
*
array
,
int
length
,
int
valueAtPosition
)
{
return
valueAtPosition
>=
0
&&
valueAtPosition
<
length
&&
array
[
valueAtPosition
]
!=
valueAtPosition
;
}
This diff is collapsed.
Click to expand it.
main.cpp
+
10
−
10
View file @
4cb74637
#include
<iostream>
#include
<iostream>
int
f
l
(
int
*
i
,
int
l
);
int
f
indeLueckeInArray
(
int
*
array
,
int
length
);
void
expect
(
bool
b
,
std
::
string
text
)
{
void
expect
(
bool
b
,
std
::
string
text
)
{
if
(
!
b
)
{
if
(
!
b
)
{
...
@@ -23,15 +23,15 @@ int main() {
...
@@ -23,15 +23,15 @@ int main() {
(
int
[])
{
4
,
-
1
,
1
,
2
,
3
}
(
int
[])
{
4
,
-
1
,
1
,
2
,
3
}
};
};
expect
(
f
l
(
v
[
0
],
3
)
==
3
,
"Expected to be 3"
);
expect
(
f
indeLueckeInArray
(
v
[
0
],
3
)
==
3
,
"Expected to be 3"
);
expect
(
f
l
(
v
[
1
],
2
)
==
0
,
"Expected to be 0"
);
expect
(
f
indeLueckeInArray
(
v
[
1
],
2
)
==
0
,
"Expected to be 0"
);
expect
(
f
l
(
v
[
2
],
3
)
==
1
,
"Expected to be 1"
);
expect
(
f
indeLueckeInArray
(
v
[
2
],
3
)
==
1
,
"Expected to be 1"
);
expect
(
f
l
(
v
[
3
],
5
)
==
4
,
"Expected to be 4"
);
expect
(
f
indeLueckeInArray
(
v
[
3
],
5
)
==
4
,
"Expected to be 4"
);
expect
(
f
l
(
v
[
4
],
5
)
==
4
,
"Expected to be 4"
);
expect
(
f
indeLueckeInArray
(
v
[
4
],
5
)
==
4
,
"Expected to be 4"
);
expect
(
f
l
(
v
[
5
],
5
)
==
0
,
"Expected to be 0"
);
expect
(
f
indeLueckeInArray
(
v
[
5
],
5
)
==
0
,
"Expected to be 0"
);
expect
(
f
l
(
v
[
6
],
5
)
==
4
,
"Expected to be 4"
);
expect
(
f
indeLueckeInArray
(
v
[
6
],
5
)
==
4
,
"Expected to be 4"
);
expect
(
f
l
(
v
[
7
],
5
)
==
5
,
"Expected to be 5"
);
expect
(
f
indeLueckeInArray
(
v
[
7
],
5
)
==
5
,
"Expected to be 5"
);
expect
(
f
l
(
v
[
8
],
5
)
==
0
,
"Expected to be 0"
);
expect
(
f
indeLueckeInArray
(
v
[
8
],
5
)
==
0
,
"Expected to be 0"
);
std
::
cout
<<
"Success!"
;
std
::
cout
<<
"Success!"
;
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment