Replace Negatives with 0
Replace all negative elements with 0.
IntermediatePhase 4 - Level 3: TransformationExample 3 of 10
replace-negatives-with-0.py
1# Take array2n = int(input("Enter array size: "))3arr = []4for i in range(n):5 arr.append(int(input(f"Element {i+1}: ")))67# Replace negatives8for i in range(len(arr)):9 if arr[i] < 0:10 arr[i] = 01112print(f"Modified array: {arr}")
Output
Enter array size: 5 Element 1: 5 Element 2: -3 Element 3: 10 Element 4: -2 Element 5: 7 Modified array: [5, 0, 10, 0, 7]
What's going on
Modify array in-place.
Key Concepts:
Check each element
Replace if negative
In-place modification
🔗Related Content
- 📖
Phase 4 - Learn Concepts
Review Phase 4 concepts and explanations
- 📝
Phase 4 - Quiz
Test your Phase 4 understanding with quiz questions
- 💻
Phase 4 - All Practice Problems
Explore all practice problems for Phase 4
- 🎓
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- 🧠
Logic Building Overview
Learn about the complete logic building curriculum